22#include "llvm/IR/DataLayout.h"
23#include "llvm/Support/Error.h"
28#include "mlir/Dialect/LLVMIR/LLVMOpsDialect.cpp.inc"
35struct LLVMSCFToControlFlowInterface : SCFToControlFlowDialectInterface {
36 using SCFToControlFlowDialectInterface::SCFToControlFlowDialectInterface;
38 bool shouldPropagateLoopAttribute(NamedAttribute attr)
const override {
39 return isa<LoopAnnotationAttr>(attr.
getValue());
44void LLVMDialect::initialize() {
45 addInterfaces<LLVMSCFToControlFlowInterface>();
48 addTypes<LLVMVoidType,
54 allowUnknownOperations();
55 declarePromisedInterface<DialectInlinerInterface, LLVMDialect>();
59LogicalResult LLVMDialect::verifyDataLayoutString(
62 llvm::DataLayout::parse(descr);
67 llvm::raw_string_ostream messageStream(message);
68 llvm::logAllUnhandledErrors(maybeDataLayout.takeError(), messageStream);
69 reportError(
"invalid data layout descriptor: " + message);
74LogicalResult LLVMDialect::verifyOperationAttribute(
Operation *op,
80 if (attr.
getName() != LLVM::LLVMDialect::getDataLayoutAttrName())
82 if (
auto stringAttr = llvm::dyn_cast<StringAttr>(attr.
getValue()))
83 return verifyDataLayoutString(
84 stringAttr.getValue(),
85 [op](
const Twine &message) { op->emitOpError() << message.str(); });
88 << LLVM::LLVMDialect::getDataLayoutAttrName()
89 <<
"' to be a string attributes";
92LogicalResult LLVMDialect::verifyParameterAttribute(
Operation *op,
100 StringAttr name = paramAttr.
getName();
102 auto checkUnitAttrType = [&]() -> LogicalResult {
103 if (!llvm::isa<UnitAttr>(paramAttr.
getValue()))
104 return op->
emitError() << name <<
" should be a unit attribute";
107 auto checkTypeAttrType = [&]() -> LogicalResult {
108 if (!llvm::isa<TypeAttr>(paramAttr.
getValue()))
109 return op->
emitError() << name <<
" should be a type attribute";
112 auto checkIntegerAttrType = [&]() -> LogicalResult {
113 if (!llvm::isa<IntegerAttr>(paramAttr.
getValue()))
114 return op->
emitError() << name <<
" should be an integer attribute";
117 auto checkPointerType = [&]() -> LogicalResult {
118 if (!llvm::isa<LLVMPointerType>(paramType))
120 << name <<
" attribute attached to non-pointer LLVM type";
123 auto checkIntegerType = [&]() -> LogicalResult {
124 if (!llvm::isa<IntegerType>(paramType))
126 << name <<
" attribute attached to non-integer LLVM type";
129 auto checkPointerTypeMatches = [&]() -> LogicalResult {
130 if (
failed(checkPointerType()))
137 if (name == LLVMDialect::getNoAliasAttrName() ||
138 name == LLVMDialect::getReadonlyAttrName() ||
139 name == LLVMDialect::getReadnoneAttrName() ||
140 name == LLVMDialect::getWriteOnlyAttrName() ||
141 name == LLVMDialect::getNestAttrName() ||
142 name == LLVMDialect::getNoCaptureAttrName() ||
143 name == LLVMDialect::getNoFreeAttrName() ||
144 name == LLVMDialect::getNoFreeObjAttrName() ||
145 name == LLVMDialect::getNonNullAttrName()) {
146 if (
failed(checkUnitAttrType()))
148 if (verifyValueType &&
failed(checkPointerType()))
154 if (name == LLVMDialect::getStructRetAttrName() ||
155 name == LLVMDialect::getByValAttrName() ||
156 name == LLVMDialect::getByRefAttrName() ||
157 name == LLVMDialect::getElementTypeAttrName() ||
158 name == LLVMDialect::getInAllocaAttrName() ||
159 name == LLVMDialect::getPreallocatedAttrName()) {
160 if (
failed(checkTypeAttrType()))
162 if (verifyValueType &&
failed(checkPointerTypeMatches()))
168 if (name == LLVMDialect::getSExtAttrName() ||
169 name == LLVMDialect::getZExtAttrName() ||
170 name == LLVMDialect::getNoExtAttrName()) {
171 if (
failed(checkUnitAttrType()))
173 if (verifyValueType &&
failed(checkIntegerType()))
179 if (name == LLVMDialect::getAlignAttrName() ||
180 name == LLVMDialect::getDereferenceableAttrName() ||
181 name == LLVMDialect::getDereferenceableOrNullAttrName()) {
182 if (
failed(checkIntegerAttrType()))
184 if (verifyValueType &&
failed(checkPointerType()))
190 if (name == LLVMDialect::getStackAlignmentAttrName()) {
191 if (
failed(checkIntegerAttrType()))
197 if (name == LLVMDialect::getNoUndefAttrName() ||
198 name == LLVMDialect::getInRegAttrName() ||
199 name == LLVMDialect::getReturnedAttrName())
200 return checkUnitAttrType();
206LogicalResult LLVMDialect::verifyRegionArgAttribute(
Operation *op,
210 auto funcOp = dyn_cast<FunctionOpInterface>(op);
213 Type argType = funcOp.getArgumentTypes()[argIdx];
215 return verifyParameterAttribute(op, argType, argAttr);
218LogicalResult LLVMDialect::verifyRegionResultAttribute(
Operation *op,
222 auto funcOp = dyn_cast<FunctionOpInterface>(op);
225 Type resType = funcOp.getResultTypes()[resIdx];
229 if (llvm::isa<LLVMVoidType>(resType))
230 return op->
emitError() <<
"cannot attach result attributes to functions "
231 "with a void return";
236 if (name == LLVMDialect::getAllocAlignAttrName() ||
237 name == LLVMDialect::getAllocatedPointerAttrName() ||
238 name == LLVMDialect::getByValAttrName() ||
239 name == LLVMDialect::getByRefAttrName() ||
240 name == LLVMDialect::getInAllocaAttrName() ||
241 name == LLVMDialect::getNestAttrName() ||
242 name == LLVMDialect::getNoCaptureAttrName() ||
243 name == LLVMDialect::getNoFreeAttrName() ||
244 name == LLVMDialect::getPreallocatedAttrName() ||
245 name == LLVMDialect::getReadnoneAttrName() ||
246 name == LLVMDialect::getReadonlyAttrName() ||
247 name == LLVMDialect::getReturnedAttrName() ||
248 name == LLVMDialect::getStackAlignmentAttrName() ||
249 name == LLVMDialect::getStructRetAttrName() ||
250 name == LLVMDialect::getWriteOnlyAttrName())
251 return op->
emitError() << name <<
" is not a valid result attribute";
252 return verifyParameterAttribute(op, resType, resAttr);
260 if (
auto symbol = dyn_cast<FlatSymbolRefAttr>(value))
261 if (isa<LLVM::LLVMPointerType>(type))
262 return LLVM::AddressOfOp::create(builder, loc, type, symbol);
263 if (isa<LLVM::UndefAttr>(value))
264 return LLVM::UndefOp::create(builder, loc, type);
265 if (isa<LLVM::PoisonAttr>(value))
266 return LLVM::PoisonOp::create(builder, loc, type);
267 if (isa<LLVM::ZeroAttr>(value))
268 return LLVM::ZeroOp::create(builder, loc, type);
269 if (isa<LLVM::MDStringAttr, LLVM::MDConstantAttr, LLVM::MDGlobalValueAttr,
270 LLVM::MDNodeAttr>(value))
271 if (isa<LLVM::LLVMMetadataType>(type))
272 return LLVM::MetadataAsValueOp::create(builder, loc, type, value);
274 return LLVM::ConstantOp::materialize(builder, value, type, loc);
Attributes are known-constant values of operations.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
NamedAttribute represents a combination of a name and an Attribute value.
StringAttr getName() const
Return the name of the attribute.
Attribute getValue() const
Return the value of the attribute.
This class helps build Operations.
Operation is the basic unit of execution within MLIR.
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
void addBytecodeInterface(LLVMDialect *dialect)
Add the interfaces necessary for encoding the LLVM dialect components in bytecode.
bool isCompatibleType(Type type)
Returns true if the given type is compatible with the LLVM dialect.
Include the generated interface declarations.