MLIR 24.0.0git
AttrToLLVMConverter.h
Go to the documentation of this file.
1//===- AttrToLLVMConverter.h - Arith attributes conversion ------*- C++ -*-===//
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#ifndef MLIR_CONVERSION_ARITHATTRTOLLVM_ATTRTOLLVMCONVERTER_H
10#define MLIR_CONVERSION_ARITHATTRTOLLVM_ATTRTOLLVMCONVERTER_H
11
14
15//===----------------------------------------------------------------------===//
16// Support for converting Arith FastMathFlags to LLVM FastmathFlags
17//===----------------------------------------------------------------------===//
18
19namespace mlir {
20namespace arith {
21/// Maps arithmetic fastmath enum values to LLVM enum values.
22LLVM::FastmathFlags
23convertArithFastMathFlagsToLLVM(arith::FastMathFlags arithFMF);
24
25/// Creates an LLVM fastmath attribute from a given arithmetic fastmath
26/// attribute.
27LLVM::FastmathFlagsAttr
28convertArithFastMathAttrToLLVM(arith::FastMathFlagsAttr fmfAttr);
29
30/// Maps arithmetic overflow enum values to LLVM enum values.
31LLVM::IntegerOverflowFlags
32convertArithOverflowFlagsToLLVM(arith::IntegerOverflowFlags arithFlags);
33
34/// Creates an LLVM rounding mode enum value from a given arithmetic rounding
35/// mode enum value.
36LLVM::RoundingMode
37convertArithRoundingModeToLLVM(arith::RoundingMode roundingMode);
38
39/// Creates an LLVM rounding mode attribute from a given arithmetic rounding
40/// mode attribute.
41LLVM::RoundingModeAttr
42convertArithRoundingModeAttrToLLVM(arith::RoundingModeAttr roundingModeAttr);
43
44/// Returns an attribute for the default LLVM FP exception behavior.
45LLVM::FPExceptionBehaviorAttr
47
48// Convert the source fastmath attribute to LLVM properties, keeping the source
49// discardable attributes separate.
50template <typename SourceOp, typename TargetOp>
52public:
54 : context(srcOp.getOperation()->getContext()),
55 convertedAttr(srcOp->getDiscardableAttrDictionary()) {
56 auto arithFMFAttr = srcOp.getFastMathFlagsAttr();
57 if (arithFMFAttr) {
58 StringRef targetAttrName = TargetOp::getFastmathAttrName();
59 Builder builder(context);
60 propertiesAttr = builder.getDictionaryAttr(builder.getNamedAttr(
61 targetAttrName, convertArithFastMathAttrToLLVM(arithFMFAttr)));
62 }
63 }
65 return convertedAttr.getAttrs();
66 }
68 Attribute getPropAttr() const { return propertiesAttr; }
69
70 typename TargetOp::Properties getProperties() const {
71 typename TargetOp::Properties properties{};
72 TargetOp::populateDefaultProperties(
73 OperationName(TargetOp::getOperationName(), context), properties);
74 if (propertiesAttr) {
75 LogicalResult result =
76 TargetOp::setPropertiesFromAttr(properties, propertiesAttr, [&]() {
77 return emitError(UnknownLoc::get(context));
78 });
79 assert(succeeded(result) && "failed to convert target properties");
80 (void)result;
81 }
82 return properties;
83 }
84
85private:
86 MLIRContext *context;
87 NamedAttrList convertedAttr;
88 DictionaryAttr propertiesAttr;
89};
90
91// Attribute converter that populates a NamedAttrList by removing the overflow
92// attribute from the source operation attributes, and replacing it with an
93// equivalent LLVM overflow attribute.
94template <typename SourceOp, typename TargetOp>
96public:
98 : convertedAttr(srcOp->getDiscardableAttrDictionary()) {
99 using IntegerOverflowFlagsAttr = LLVM::IntegerOverflowFlagsAttr;
100
101 if (auto arithAttr = srcOp.getOverflowAttr()) {
102 auto llvmFlag = convertArithOverflowFlagsToLLVM(arithAttr.getValue());
103 // Create a dictionary attribute holding the overflow flags property.
104 // (In the LLVM dialect, the overflow flags are a property, not an
105 // attribute.)
106 MLIRContext *ctx = srcOp.getOperation()->getContext();
107 Builder b(ctx);
108 auto llvmFlagAttr = IntegerOverflowFlagsAttr::get(ctx, llvmFlag);
109 StringRef llvmAttrName = TargetOp::getOverflowFlagsAttrName();
110 NamedAttribute attr{llvmAttrName, llvmFlagAttr};
111 // Set the properties attribute of the operation state so that the
112 // property can be updated when the operation is created.
113 propertiesAttr = b.getDictionaryAttr(ArrayRef(attr));
114 }
115 }
116 ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
117 Attribute getPropAttr() const { return propertiesAttr; }
118
119private:
120 NamedAttrList convertedAttr;
121 DictionaryAttr propertiesAttr;
122};
123
124// Attribute converter that populates a NamedAttrList by removing the nonNeg
125// attribute from the source operation attributes, and setting it as a property
126// on the target LLVM operation.
127template <typename SourceOp, typename TargetOp>
129public:
131 : convertedAttr(srcOp->getDiscardableAttrDictionary()) {
132 if (!srcOp.getNonNeg())
133 return;
134 MLIRContext *ctx = srcOp.getOperation()->getContext();
135 Builder b(ctx);
136 NamedAttribute attr{"nonNeg", b.getUnitAttr()};
137 propertiesAttr = b.getDictionaryAttr(ArrayRef(attr));
138 }
139 ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
140 Attribute getPropAttr() const { return propertiesAttr; }
141
142private:
143 NamedAttrList convertedAttr;
144 DictionaryAttr propertiesAttr;
145};
146
147template <typename SourceOp, typename TargetOp>
149 static_assert(TargetOp::template hasTrait<
150 LLVM::FPExceptionBehaviorOpInterface::Trait>(),
151 "Target constrained FP operations must implement "
152 "LLVM::FPExceptionBehaviorOpInterface");
153
154public:
156 : convertedAttr(srcOp->getDiscardableAttrDictionary()) {
157 if constexpr (TargetOp::template hasTrait<
158 LLVM::RoundingModeOpInterface::Trait>()) {
159 auto arithAttr = srcOp.getRoundingModeAttr();
160 convertedAttr.set(TargetOp::getRoundingModeAttrName(),
162 }
163 // Constrained intrinsics (llvm.intr.experimental.constrained.*) do not
164 // support fastmath flags, so do not copy them from the source operation.
165 convertedAttr.set(TargetOp::getFPExceptionBehaviorAttrName(),
166 getLLVMDefaultFPExceptionBehavior(*srcOp->getContext()));
167 }
168
169 ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
170 Attribute getPropAttr() const { return {}; }
171
172private:
173 NamedAttrList convertedAttr;
174};
175} // namespace arith
176} // namespace mlir
177
178#endif // MLIR_CONVERSION_ARITHATTRTOLLVM_ATTRTOLLVMCONVERTER_H
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
b getContext())
Attributes are known-constant values of operations.
Definition Attributes.h:25
This class is a general helper class for creating context-global objects like types,...
Definition Builders.h:51
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
Definition Builders.cpp:112
NamedAttribute getNamedAttr(StringRef name, Attribute val)
Definition Builders.cpp:102
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:64
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
NamedAttribute represents a combination of a name and an Attribute value.
Definition Attributes.h:164
TargetOp::Properties getProperties() const
ArrayRef< NamedAttribute > getDiscardableAttrs() const
ArrayRef< NamedAttribute > getAttrs() const
ArrayRef< NamedAttribute > getAttrs() const
ArrayRef< NamedAttribute > getAttrs() const
ArrayRef< NamedAttribute > getAttrs() const
LLVM::FPExceptionBehaviorAttr getLLVMDefaultFPExceptionBehavior(MLIRContext &context)
Returns an attribute for the default LLVM FP exception behavior.
LLVM::FastmathFlagsAttr convertArithFastMathAttrToLLVM(arith::FastMathFlagsAttr fmfAttr)
Creates an LLVM fastmath attribute from a given arithmetic fastmath attribute.
LLVM::RoundingMode convertArithRoundingModeToLLVM(arith::RoundingMode roundingMode)
Creates an LLVM rounding mode enum value from a given arithmetic rounding mode enum value.
LLVM::IntegerOverflowFlags convertArithOverflowFlagsToLLVM(arith::IntegerOverflowFlags arithFlags)
Maps arithmetic overflow enum values to LLVM enum values.
LLVM::FastmathFlags convertArithFastMathFlagsToLLVM(arith::FastMathFlags arithFMF)
Maps arithmetic fastmath enum values to LLVM enum values.
LLVM::RoundingModeAttr convertArithRoundingModeAttrToLLVM(arith::RoundingModeAttr roundingModeAttr)
Creates an LLVM rounding mode attribute from a given arithmetic rounding mode attribute.
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.