MLIR 22.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
114template <typename SourceOp, typename TargetOp>
116 static_assert(TargetOp::template hasTrait<
117 LLVM::FPExceptionBehaviorOpInterface::Trait>(),
118 "Target constrained FP operations must implement "
119 "LLVM::FPExceptionBehaviorOpInterface");
120
121public:
123 // Copy the source attributes.
124 convertedAttr = NamedAttrList{srcOp->getAttrs()};
125
126 if constexpr (TargetOp::template hasTrait<
127 LLVM::RoundingModeOpInterface::Trait>()) {
128 // Get the name of the rounding mode attribute.
129 StringRef arithAttrName = srcOp.getRoundingModeAttrName();
130 // Remove the source attribute.
131 auto arithAttr =
132 cast<arith::RoundingModeAttr>(convertedAttr.erase(arithAttrName));
133 // Set the target attribute.
134 convertedAttr.set(TargetOp::getRoundingModeAttrName(),
136 }
137 convertedAttr.set(TargetOp::getFPExceptionBehaviorAttrName(),
138 getLLVMDefaultFPExceptionBehavior(*srcOp->getContext()));
139 }
140
141 ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
142 Attribute getPropAttr() const { return {}; }
143
144private:
145 NamedAttrList convertedAttr;
146};
147
148} // namespace arith
149} // namespace mlir
150
151#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
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.