1//===- LLVMDialect.cpp - LLVM IR dialect registration ---------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the LLVM IR dialect in MLIR and registers it. The ops are
10// defined by the source file of their op partition, e.g. LLVMArithOps.cpp.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/Dialect/LLVMIR/LLVMDialectDecl.h"
15
16#include "LLVMDialectBytecode.h"
17#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlowDialectInterface.h"
18#include "mlir/Dialect/LLVMIR/LLVMConstantOps.h"
19#include "mlir/IR/DialectImplementation.h"
20#include "mlir/Interfaces/FunctionInterfaces.h"
21#include "mlir/Transforms/InliningUtils.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/Support/Error.h"
24
25using namespace mlir;
26using namespace mlir::LLVM;
27
28#include "mlir/Dialect/LLVMIR/LLVMOpsDialect.cpp.inc"
29
30//===----------------------------------------------------------------------===//
31// LLVMDialect initialization, type parsing, and registration.
32//===----------------------------------------------------------------------===//
33
34namespace {
35struct LLVMSCFToControlFlowInterface : SCFToControlFlowDialectInterface {
36 using SCFToControlFlowDialectInterface::SCFToControlFlowDialectInterface;
37
38 bool shouldPropagateLoopAttribute(NamedAttribute attr) const override {
39 return isa<LoopAnnotationAttr>(Val: attr.getValue());
40 }
41};
42} // namespace
43
44void LLVMDialect::initialize() {
45 addInterfaces<LLVMSCFToControlFlowInterface>();
46 registerAttributes();
47
48 // clang-format off
49 addTypes<LLVMVoidType,
50 LLVMLabelType,
51 LLVMMetadataType>();
52 // clang-format on
53 registerTypes();
54
55 registerLLVMDialectOperations(dialect: this);
56
57 // Support unknown operations because not all LLVM operations are registered.
58 allowUnknownOperations();
59 declarePromisedInterface<DialectInlinerInterface, LLVMDialect>();
60 detail::addBytecodeInterface(dialect: this);
61}
62
63LogicalResult LLVMDialect::verifyDataLayoutString(
64 StringRef descr, llvm::function_ref<void(const Twine &)> reportError) {
65 llvm::Expected<llvm::DataLayout> maybeDataLayout =
66 llvm::DataLayout::parse(LayoutString: descr);
67 if (maybeDataLayout)
68 return success();
69
70 std::string message;
71 llvm::raw_string_ostream messageStream(message);
72 llvm::logAllUnhandledErrors(E: maybeDataLayout.takeError(), OS&: messageStream);
73 reportError("invalid data layout descriptor: " + message);
74 return failure();
75}
76
77/// Verify LLVM dialect attributes.
78LogicalResult LLVMDialect::verifyOperationAttribute(Operation *op,
79 NamedAttribute attr) {
80 // If the data layout attribute is present, it must use the LLVM data layout
81 // syntax. Try parsing it and report errors in case of failure. Users of this
82 // attribute may assume it is well-formed and can pass it to the (asserting)
83 // llvm::DataLayout constructor.
84 if (attr.getName() != LLVM::LLVMDialect::getDataLayoutAttrName())
85 return success();
86 if (auto stringAttr = llvm::dyn_cast<StringAttr>(Val: attr.getValue()))
87 return verifyDataLayoutString(
88 descr: stringAttr.getValue(),
89 reportError: [op](const Twine &message) { op->emitOpError() << message.str(); });
90
91 return op->emitOpError() << "expected '"
92 << LLVM::LLVMDialect::getDataLayoutAttrName()
93 << "' to be a string attributes";
94}
95
96LogicalResult LLVMDialect::verifyParameterAttribute(Operation *op,
97 Type paramType,
98 NamedAttribute paramAttr) {
99 // LLVM attribute may be attached to a result of operation that has not been
100 // converted to LLVM dialect yet, so the result may have a type with unknown
101 // representation in LLVM dialect type space. In this case we cannot verify
102 // whether the attribute may be
103 bool verifyValueType = isCompatibleType(paramType);
104 StringAttr name = paramAttr.getName();
105
106 auto checkUnitAttrType = [&]() -> LogicalResult {
107 if (!llvm::isa<UnitAttr>(Val: paramAttr.getValue()))
108 return op->emitError() << name << " should be a unit attribute";
109 return success();
110 };
111 auto checkTypeAttrType = [&]() -> LogicalResult {
112 if (!llvm::isa<TypeAttr>(Val: paramAttr.getValue()))
113 return op->emitError() << name << " should be a type attribute";
114 return success();
115 };
116 auto checkIntegerAttrType = [&]() -> LogicalResult {
117 if (!llvm::isa<IntegerAttr>(Val: paramAttr.getValue()))
118 return op->emitError() << name << " should be an integer attribute";
119 return success();
120 };
121 auto checkPointerType = [&]() -> LogicalResult {
122 if (!llvm::isa<LLVMPointerType>(Val: paramType))
123 return op->emitError()
124 << name << " attribute attached to non-pointer LLVM type";
125 return success();
126 };
127 auto checkIntegerType = [&]() -> LogicalResult {
128 if (!llvm::isa<IntegerType>(Val: paramType))
129 return op->emitError()
130 << name << " attribute attached to non-integer LLVM type";
131 return success();
132 };
133 auto checkPointerTypeMatches = [&]() -> LogicalResult {
134 if (failed(Result: checkPointerType()))
135 return failure();
136
137 return success();
138 };
139
140 // Check a unit attribute that is attached to a pointer value.
141 if (name == LLVMDialect::getNoAliasAttrName() ||
142 name == LLVMDialect::getReadonlyAttrName() ||
143 name == LLVMDialect::getReadnoneAttrName() ||
144 name == LLVMDialect::getWriteOnlyAttrName() ||
145 name == LLVMDialect::getNestAttrName() ||
146 name == LLVMDialect::getNoCaptureAttrName() ||
147 name == LLVMDialect::getNoFreeAttrName() ||
148 name == LLVMDialect::getNoFreeObjAttrName() ||
149 name == LLVMDialect::getNonNullAttrName()) {
150 if (failed(Result: checkUnitAttrType()))
151 return failure();
152 if (verifyValueType && failed(Result: checkPointerType()))
153 return failure();
154 return success();
155 }
156
157 // Check a type attribute that is attached to a pointer value.
158 if (name == LLVMDialect::getStructRetAttrName() ||
159 name == LLVMDialect::getByValAttrName() ||
160 name == LLVMDialect::getByRefAttrName() ||
161 name == LLVMDialect::getElementTypeAttrName() ||
162 name == LLVMDialect::getInAllocaAttrName() ||
163 name == LLVMDialect::getPreallocatedAttrName()) {
164 if (failed(Result: checkTypeAttrType()))
165 return failure();
166 if (verifyValueType && failed(Result: checkPointerTypeMatches()))
167 return failure();
168 return success();
169 }
170
171 // Check a unit attribute that is attached to an integer value.
172 if (name == LLVMDialect::getSExtAttrName() ||
173 name == LLVMDialect::getZExtAttrName() ||
174 name == LLVMDialect::getNoExtAttrName()) {
175 if (failed(Result: checkUnitAttrType()))
176 return failure();
177 if (verifyValueType && failed(Result: checkIntegerType()))
178 return failure();
179 return success();
180 }
181
182 // Check an integer attribute that is attached to a pointer value.
183 if (name == LLVMDialect::getAlignAttrName() ||
184 name == LLVMDialect::getDereferenceableAttrName() ||
185 name == LLVMDialect::getDereferenceableOrNullAttrName()) {
186 if (failed(Result: checkIntegerAttrType()))
187 return failure();
188 if (verifyValueType && failed(Result: checkPointerType()))
189 return failure();
190 return success();
191 }
192
193 // Check an integer attribute that is attached to a pointer value.
194 if (name == LLVMDialect::getStackAlignmentAttrName()) {
195 if (failed(Result: checkIntegerAttrType()))
196 return failure();
197 return success();
198 }
199
200 // Check a unit attribute that can be attached to arbitrary types.
201 if (name == LLVMDialect::getNoUndefAttrName() ||
202 name == LLVMDialect::getInRegAttrName() ||
203 name == LLVMDialect::getReturnedAttrName())
204 return checkUnitAttrType();
205
206 return success();
207}
208
209/// Verify LLVMIR function argument attributes.
210LogicalResult LLVMDialect::verifyRegionArgAttribute(Operation *op,
211 unsigned regionIdx,
212 unsigned argIdx,
213 NamedAttribute argAttr) {
214 auto funcOp = dyn_cast<FunctionOpInterface>(Val: op);
215 if (!funcOp)
216 return success();
217 Type argType = funcOp.getArgumentTypes()[argIdx];
218
219 return verifyParameterAttribute(op, paramType: argType, paramAttr: argAttr);
220}
221
222LogicalResult LLVMDialect::verifyRegionResultAttribute(Operation *op,
223 unsigned regionIdx,
224 unsigned resIdx,
225 NamedAttribute resAttr) {
226 auto funcOp = dyn_cast<FunctionOpInterface>(Val: op);
227 if (!funcOp)
228 return success();
229 Type resType = funcOp.getResultTypes()[resIdx];
230
231 // Check to see if this function has a void return with a result attribute
232 // to it. It isn't clear what semantics we would assign to that.
233 if (llvm::isa<LLVMVoidType>(Val: resType))
234 return op->emitError() << "cannot attach result attributes to functions "
235 "with a void return";
236
237 // Check to see if this attribute is allowed as a result attribute. Only
238 // explicitly forbidden LLVM attributes will cause an error.
239 auto name = resAttr.getName();
240 if (name == LLVMDialect::getAllocAlignAttrName() ||
241 name == LLVMDialect::getAllocatedPointerAttrName() ||
242 name == LLVMDialect::getByValAttrName() ||
243 name == LLVMDialect::getByRefAttrName() ||
244 name == LLVMDialect::getInAllocaAttrName() ||
245 name == LLVMDialect::getNestAttrName() ||
246 name == LLVMDialect::getNoCaptureAttrName() ||
247 name == LLVMDialect::getNoFreeAttrName() ||
248 name == LLVMDialect::getPreallocatedAttrName() ||
249 name == LLVMDialect::getReadnoneAttrName() ||
250 name == LLVMDialect::getReadonlyAttrName() ||
251 name == LLVMDialect::getReturnedAttrName() ||
252 name == LLVMDialect::getStackAlignmentAttrName() ||
253 name == LLVMDialect::getStructRetAttrName() ||
254 name == LLVMDialect::getWriteOnlyAttrName())
255 return op->emitError() << name << " is not a valid result attribute";
256 return verifyParameterAttribute(op, paramType: resType, paramAttr: resAttr);
257}
258
259Operation *LLVMDialect::materializeConstant(OpBuilder &builder, Attribute value,
260 Type type, Location loc) {
261 // If this was folded from an operation other than llvm.mlir.constant, it
262 // should be materialized as such. Note that an llvm.mlir.zero may fold into
263 // a builtin zero attribute and thus will materialize as a llvm.mlir.constant.
264 if (auto symbol = dyn_cast<FlatSymbolRefAttr>(Val&: value))
265 if (isa<LLVM::LLVMPointerType>(Val: type))
266 return LLVM::AddressOfOp::create(builder, location: loc, res: type, global_name: symbol);
267 if (isa<LLVM::UndefAttr>(Val: value))
268 return LLVM::UndefOp::create(builder, location: loc, res: type);
269 if (isa<LLVM::PoisonAttr>(Val: value))
270 return LLVM::PoisonOp::create(builder, location: loc, res: type);
271 if (isa<LLVM::ZeroAttr>(Val: value))
272 return LLVM::ZeroOp::create(builder, location: loc, res: type);
273 if (isa<LLVM::MDStringAttr, LLVM::MDConstantAttr, LLVM::MDGlobalValueAttr,
274 LLVM::MDNodeAttr>(Val: value))
275 if (isa<LLVM::LLVMMetadataType>(Val: type))
276 return LLVM::MetadataAsValueOp::create(builder, location: loc, res: type, metadata: value);
277 // Otherwise try materializing it as a regular llvm.mlir.constant op.
278 return LLVM::ConstantOp::materialize(builder, value, type, loc);
279}
280