Skip to content

Commit 3b756e4

Browse files
committed
Extend
1 parent f144e15 commit 3b756e4

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

mlir/include/mlir/Conversion/ArithCommon/AttrToLLVMConverter.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,41 @@ class AttrConvertOverflowToLLVM {
103103
private:
104104
NamedAttrList convertedAttr;
105105
};
106+
107+
template <typename SourceOp, typename TargetOp>
108+
class AttrConverterConstrainedFPToLLVM {
109+
static_assert(TargetOp::template hasTrait<
110+
LLVM::FPExceptionBehaviorOpInterface::Trait>(),
111+
"Target constrained FP operations must implement "
112+
"LLVM::FPExceptionBehaviorOpInterface");
113+
114+
public:
115+
AttrConverterConstrainedFPToLLVM(
116+
SourceOp srcOp) {
117+
// Copy the source attributes.
118+
convertedAttr = NamedAttrList{srcOp->getAttrs()};
119+
120+
if constexpr (TargetOp::template hasTrait<
121+
LLVM::RoundingModeOpInterface::Trait>()) {
122+
// Get the name of the rounding mode attribute.
123+
StringRef arithAttrName = srcOp.getRoundingModeAttrName();
124+
// Remove the source attribute.
125+
auto arithAttr =
126+
cast<arith::RoundingModeAttr>(convertedAttr.erase(arithAttrName));
127+
// Set the target attribute.
128+
convertedAttr.set(TargetOp::getRoundingModeAttrName(),
129+
convertArithRoundingModeAttrToLLVM(arithAttr));
130+
}
131+
convertedAttr.set(TargetOp::getFPExceptionBehaviorAttrName(),
132+
getLLVMDefaultFPExceptionBehavior(*srcOp->getContext()));
133+
}
134+
135+
ArrayRef<NamedAttribute> getAttrs() const { return convertedAttr.getAttrs(); }
136+
137+
private:
138+
NamedAttrList convertedAttr;
139+
};
140+
106141
} // namespace arith
107142
} // namespace mlir
108143

mlir/lib/Conversion/ArithToLLVM/ArithToLLVM.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ using SubIOpLowering =
139139
using TruncFOpLowering =
140140
ConstrainedVectorConvertToLLVMPattern<arith::TruncFOp, LLVM::FPTruncOp,
141141
false>;
142+
using ConstrainedTruncFOpLowering = ConstrainedVectorConvertToLLVMPattern<
143+
arith::TruncFOp, LLVM::ConstrainedFPTruncIntr, true,
144+
arith::AttrConverterConstrainedFPToLLVM>;
142145
using TruncIOpLowering =
143146
VectorConvertToLLVMPattern<arith::TruncIOp, LLVM::TruncOp>;
144147
using UIToFPOpLowering =
@@ -563,6 +566,7 @@ void mlir::arith::populateArithToLLVMConversionPatterns(
563566
SubFOpLowering,
564567
SubIOpLowering,
565568
TruncFOpLowering,
569+
ConstrainedTruncFOpLowering,
566570
TruncIOpLowering,
567571
UIToFPOpLowering,
568572
XOrIOpLowering

mlir/test/Conversion/ArithToLLVM/arith-to-llvm.mlir

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,21 @@ func.func @fptrunc_vector(%arg0 : vector<2xf32>, %arg1 : vector<2xf64>) {
289289
return
290290
}
291291

292+
// CHECK-LABEL: experimental_constrained_fptrunc
293+
func.func @experimental_constrained_fptrunc(%arg0 : f64) {
294+
// CHECK-NEXT: = llvm.intr.experimental.constrained.fptrunc {{.*}} tonearest ignore : f64 to f32
295+
%0 = arith.truncf %arg0 tonearesteven : f64 to f32
296+
// CHECK-NEXT: = llvm.intr.experimental.constrained.fptrunc {{.*}} downward ignore : f64 to f32
297+
%1 = arith.truncf %arg0 downward : f64 to f32
298+
// CHECK-NEXT: = llvm.intr.experimental.constrained.fptrunc {{.*}} upward ignore : f64 to f32
299+
%2 = arith.truncf %arg0 upward : f64 to f32
300+
// CHECK-NEXT: = llvm.intr.experimental.constrained.fptrunc {{.*}} towardzero ignore : f64 to f32
301+
%3 = arith.truncf %arg0 towardzero : f64 to f32
302+
// CHECK-NEXT: = llvm.intr.experimental.constrained.fptrunc {{.*}} tonearestaway ignore : f64 to f32
303+
%4 = arith.truncf %arg0 tonearestaway : f64 to f32
304+
return
305+
}
306+
292307
// Check sign and zero extension and truncation of integers.
293308
// CHECK-LABEL: @integer_extension_and_truncation
294309
func.func @integer_extension_and_truncation(%arg0 : i3) {

0 commit comments

Comments
 (0)