MLIR 23.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_ARITHCOMMON_ATTRTOLLVMCONVERTER_H
10#define MLIR_CONVERSION_ARITHCOMMON_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// Attribute converter that populates a NamedAttrList by removing the fastmath
49// attribute from the source operation attributes, and replacing it with an
50// equivalent LLVM fastmath attribute.
51template <typename SourceOp, typename TargetOp>
53public:
54 AttrConvertFastMathToLLVM(SourceOp srcOp) {
55 // Copy the source attributes.
56 convertedAttr = NamedAttrList{srcOp->getAttrs()};
57 // Get the name of the arith fastmath attribute.
58 StringRef arithFMFAttrName = SourceOp::getFastMathAttrName();
59 // Remove the source fastmath attribute.
60 auto arithFMFAttr = dyn_cast_if_present<arith::FastMathFlagsAttr>(
61 convertedAttr.erase(arithFMFAttrName));
62 if (arithFMFAttr) {
63 StringRef targetAttrName = TargetOp::getFastmathAttrName();
64 convertedAttr.set(targetAttrName,
65 convertArithFastMathAttrToLLVM(arithFMFAttr));
66 }
67 }
68 ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
69 Attribute getPropAttr() const { return {}; }
70
71private:
72 NamedAttrList convertedAttr;
73};
74
75// Attribute converter that populates a NamedAttrList by removing the overflow
76// attribute from the source operation attributes, and replacing it with an
77// equivalent LLVM overflow attribute.
78template <typename SourceOp, typename TargetOp>
80public:
81 AttrConvertOverflowToLLVM(SourceOp srcOp) {
82 using IntegerOverflowFlagsAttr = LLVM::IntegerOverflowFlagsAttr;
83
84 // Copy the source attributes.
85 convertedAttr = NamedAttrList{srcOp->getAttrs()};
86 // Get the name of the arith overflow attribute.
87 StringRef arithAttrName = SourceOp::getIntegerOverflowAttrName();
88 // Remove the source overflow attribute from the set that will be present
89 // in the target.
90 if (auto arithAttr = dyn_cast_if_present<arith::IntegerOverflowFlagsAttr>(
91 convertedAttr.erase(arithAttrName))) {
92 auto llvmFlag = convertArithOverflowFlagsToLLVM(arithAttr.getValue());
93 // Create a dictionary attribute holding the overflow flags property.
94 // (In the LLVM dialect, the overflow flags are a property, not an
95 // attribute.)
96 MLIRContext *ctx = srcOp.getOperation()->getContext();
97 Builder b(ctx);
98 auto llvmFlagAttr = IntegerOverflowFlagsAttr::get(ctx, llvmFlag);
99 StringRef llvmAttrName = TargetOp::getOverflowFlagsAttrName();
100 NamedAttribute attr{llvmAttrName, llvmFlagAttr};
101 // Set the properties attribute of the operation state so that the
102 // property can be updated when the operation is created.
103 propertiesAttr = b.getDictionaryAttr(ArrayRef(attr));
104 }
105 }
106 ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
107 Attribute getPropAttr() const { return propertiesAttr; }
108
109private:
110 NamedAttrList convertedAttr;
111 DictionaryAttr propertiesAttr;
112};
113
114// Attribute converter that populates a NamedAttrList by removing the nonNeg
115// attribute from the source operation attributes, and setting it as a property
116// on the target LLVM operation.
117template <typename SourceOp, typename TargetOp>
119public:
120 AttrConvertNonNegToLLVM(SourceOp srcOp) {
121 convertedAttr = NamedAttrList{srcOp->getAttrs()};
122 if (!convertedAttr.erase("nonNeg"))
123 return;
124 MLIRContext *ctx = srcOp.getOperation()->getContext();
125 Builder b(ctx);
126 NamedAttribute attr{"nonNeg", b.getUnitAttr()};
127 propertiesAttr = b.getDictionaryAttr(ArrayRef(attr));
128 }
129 ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
130 Attribute getPropAttr() const { return propertiesAttr; }
131
132private:
133 NamedAttrList convertedAttr;
134 DictionaryAttr propertiesAttr;
135};
136
137template <typename SourceOp, typename TargetOp>
139 static_assert(TargetOp::template hasTrait<
140 LLVM::FPExceptionBehaviorOpInterface::Trait>(),
141 "Target constrained FP operations must implement "
142 "LLVM::FPExceptionBehaviorOpInterface");
143
144public:
146 // Copy the source attributes.
147 convertedAttr = NamedAttrList{srcOp->getAttrs()};
148
149 if constexpr (TargetOp::template hasTrait<
150 LLVM::RoundingModeOpInterface::Trait>()) {
151 // Get the name of the rounding mode attribute.
152 StringRef arithAttrName = srcOp.getRoundingModeAttrName();
153 // Remove the source attribute.
154 auto arithAttr =
155 cast<arith::RoundingModeAttr>(convertedAttr.erase(arithAttrName));
156 // Set the target attribute.
157 convertedAttr.set(TargetOp::getRoundingModeAttrName(),
159 }
160 convertedAttr.set(TargetOp::getFPExceptionBehaviorAttrName(),
161 getLLVMDefaultFPExceptionBehavior(*srcOp->getContext()));
162 }
163
164 ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
165 Attribute getPropAttr() const { return {}; }
166
167private:
168 NamedAttrList convertedAttr;
169};
170
171} // namespace arith
172} // namespace mlir
173
174#endif // MLIR_CONVERSION_ARITHCOMMON_ATTRTOLLVMCONVERTER_H
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
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
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
Attribute set(StringAttr name, Attribute value)
If the an attribute exists with the specified name, change it to the new value.
NamedAttribute represents a combination of a name and an Attribute value.
Definition Attributes.h:164
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.