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_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
22/// Maps arithmetic fastmath enum values to LLVM enum values.
23LLVM::FastmathFlags
24convertArithFastMathFlagsToLLVM(arith::FastMathFlags arithFMF);
25
26/// Creates an LLVM fastmath attribute from a given arithmetic fastmath
27/// attribute.
28LLVM::FastmathFlagsAttr
29convertArithFastMathAttrToLLVM(arith::FastMathFlagsAttr fmfAttr);
30
31/// Maps arithmetic overflow enum values to LLVM enum values.
32LLVM::IntegerOverflowFlags
33convertArithOverflowFlagsToLLVM(arith::IntegerOverflowFlags arithFlags);
34
35/// Creates an LLVM rounding mode enum value from a given arithmetic rounding
36/// mode enum value.
37LLVM::RoundingMode
38convertArithRoundingModeToLLVM(arith::RoundingMode roundingMode);
39
40/// Creates an LLVM rounding mode attribute from a given arithmetic rounding
41/// mode attribute.
42LLVM::RoundingModeAttr
43convertArithRoundingModeAttrToLLVM(arith::RoundingModeAttr roundingModeAttr);
44
45/// Returns an attribute for the default LLVM FP exception behavior.
46LLVM::FPExceptionBehaviorAttr
48
49// Convert the source fastmath attribute to LLVM properties, keeping the source
50// discardable attributes separate.
51template <typename SourceOp, typename TargetOp>
53public:
55 : context(srcOp.getOperation()->getContext()),
56 convertedAttr(srcOp->getDiscardableAttrDictionary()) {
57 auto arithFMFAttr = srcOp.getFastMathFlagsAttr();
58 if (arithFMFAttr) {
59 StringRef targetAttrName = TargetOp::getFastmathAttrName();
60 Builder builder(context);
61 propertiesAttr = builder.getDictionaryAttr(builder.getNamedAttr(
62 targetAttrName, convertArithFastMathAttrToLLVM(arithFMFAttr)));
63 }
64 }
66 return convertedAttr.getAttrs();
67 }
69 Attribute getPropAttr() const { return propertiesAttr; }
70
71 typename TargetOp::Properties getProperties() const {
72 typename TargetOp::Properties properties{};
73 TargetOp::populateDefaultProperties(
74 OperationName(TargetOp::getOperationName(), context), properties);
75 if (propertiesAttr) {
76 LogicalResult result =
77 TargetOp::setPropertiesFromAttr(properties, propertiesAttr, [&]() {
78 return emitError(UnknownLoc::get(context));
79 });
80 assert(succeeded(result) && "failed to convert target properties");
81 (void)result;
82 }
83 return properties;
84 }
85
86private:
87 MLIRContext *context;
88 NamedAttrList convertedAttr;
89 DictionaryAttr propertiesAttr;
90};
91
92// Attribute converter that populates a NamedAttrList by removing the overflow
93// attribute from the source operation attributes, and replacing it with an
94// equivalent LLVM overflow attribute.
95template <typename SourceOp, typename TargetOp>
97public:
99 : convertedAttr(srcOp->getDiscardableAttrDictionary()) {
100 using IntegerOverflowFlagsAttr = LLVM::IntegerOverflowFlagsAttr;
101
102 if (auto arithAttr = srcOp.getOverflowAttr()) {
103 auto llvmFlag = convertArithOverflowFlagsToLLVM(arithAttr.getValue());
104 // Create a dictionary attribute holding the overflow flags property.
105 // (In the LLVM dialect, the overflow flags are a property, not an
106 // attribute.)
107 MLIRContext *ctx = srcOp.getOperation()->getContext();
108 Builder b(ctx);
109 auto llvmFlagAttr = IntegerOverflowFlagsAttr::get(ctx, llvmFlag);
110 StringRef llvmAttrName = TargetOp::getOverflowFlagsAttrName();
111 NamedAttribute attr{llvmAttrName, llvmFlagAttr};
112 // Set the properties attribute of the operation state so that the
113 // property can be updated when the operation is created.
114 propertiesAttr = b.getDictionaryAttr(ArrayRef(attr));
115 }
116 }
117 ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
118 Attribute getPropAttr() const { return propertiesAttr; }
119
120private:
121 NamedAttrList convertedAttr;
122 DictionaryAttr propertiesAttr;
123};
124
125// Attribute converter that populates a NamedAttrList by removing the nonNeg
126// attribute from the source operation attributes, and setting it as a property
127// on the target LLVM operation.
128template <typename SourceOp, typename TargetOp>
130public:
132 : convertedAttr(srcOp->getDiscardableAttrDictionary()) {
133 if (!srcOp.getNonNeg())
134 return;
135 MLIRContext *ctx = srcOp.getOperation()->getContext();
136 Builder b(ctx);
137 NamedAttribute attr{"nonNeg", b.getUnitAttr()};
138 propertiesAttr = b.getDictionaryAttr(ArrayRef(attr));
139 }
140 ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
141 Attribute getPropAttr() const { return propertiesAttr; }
142
143private:
144 NamedAttrList convertedAttr;
145 DictionaryAttr propertiesAttr;
146};
147
148template <typename SourceOp, typename TargetOp>
150 static_assert(TargetOp::template hasTrait<
151 LLVM::FPExceptionBehaviorOpInterface::Trait>(),
152 "Target constrained FP operations must implement "
153 "LLVM::FPExceptionBehaviorOpInterface");
154
155public:
157 : convertedAttr(srcOp->getDiscardableAttrDictionary()) {
158 if constexpr (TargetOp::template hasTrait<
159 LLVM::RoundingModeOpInterface::Trait>()) {
160 auto arithAttr = srcOp.getRoundingModeAttr();
161 convertedAttr.set(TargetOp::getRoundingModeAttrName(),
163 }
164 // Constrained intrinsics (llvm.intr.experimental.constrained.*) do not
165 // support fastmath flags, so do not copy them from the source operation.
166 convertedAttr.set(TargetOp::getFPExceptionBehaviorAttrName(),
167 getLLVMDefaultFPExceptionBehavior(*srcOp->getContext()));
168 }
169
170 ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
171 Attribute getPropAttr() const { return {}; }
172
173private:
174 NamedAttrList convertedAttr;
175};
176
177} // namespace arith
178} // namespace mlir
179
180#endif // MLIR_CONVERSION_ARITHCOMMON_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:63
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.