Skip to content

Commit 8af308f

Browse files
committed
[MLIR][LLVM] Add ftz and fuse FP ops related function attribute support
1 parent c119da2 commit 8af308f

File tree

6 files changed

+94
-0
lines changed

6 files changed

+94
-0
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMOps.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,9 @@ def LLVM_LLVMFuncOp : LLVM_Op<"func", [
14541454
OptionalAttr<BoolAttr>:$no_nans_fp_math,
14551455
OptionalAttr<BoolAttr>:$approx_func_fp_math,
14561456
OptionalAttr<BoolAttr>:$no_signed_zeros_fp_math,
1457+
OptionalAttr<StrAttr>:$denormal_fp_math,
1458+
OptionalAttr<StrAttr>:$denormal_fp_math_f32,
1459+
OptionalAttr<StrAttr>:$fp_contract,
14571460
OptionalAttr<UnitAttr>:$no_inline,
14581461
OptionalAttr<UnitAttr>:$always_inline,
14591462
OptionalAttr<UnitAttr>:$optimize_none

mlir/lib/Target/LLVMIR/ModuleImport.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,9 @@ static constexpr std::array kExplicitAttributes{
16761676
StringLiteral("alwaysinline"),
16771677
StringLiteral("approx-func-fp-math"),
16781678
StringLiteral("convergent"),
1679+
StringLiteral("denormal-fp-math"),
1680+
StringLiteral("denormal-fp-math-f32"),
1681+
StringLiteral("fp-contract"),
16791682
StringLiteral("frame-pointer"),
16801683
StringLiteral("no-infs-fp-math"),
16811684
StringLiteral("no-nans-fp-math"),
@@ -1823,6 +1826,18 @@ void ModuleImport::processFunctionAttributes(llvm::Function *func,
18231826
if (llvm::Attribute attr = func->getFnAttribute("no-signed-zeros-fp-math");
18241827
attr.isStringAttribute())
18251828
funcOp.setNoSignedZerosFpMath(attr.getValueAsBool());
1829+
1830+
if (llvm::Attribute attr = func->getFnAttribute("denormal-fp-math");
1831+
attr.isStringAttribute())
1832+
funcOp.setDenormalFpMathAttr(StringAttr::get(context, attr.getValueAsString()));
1833+
1834+
if (llvm::Attribute attr = func->getFnAttribute("denormal-fp-math-f32");
1835+
attr.isStringAttribute())
1836+
funcOp.setDenormalFpMathF32Attr(StringAttr::get(context, attr.getValueAsString()));
1837+
1838+
if (llvm::Attribute attr = func->getFnAttribute("fp-contract");
1839+
attr.isStringAttribute())
1840+
funcOp.setFpContractAttr(StringAttr::get(context, attr.getValueAsString()));
18261841
}
18271842

18281843
DictionaryAttr

mlir/lib/Target/LLVMIR/ModuleTranslation.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,18 @@ LogicalResult ModuleTranslation::convertOneFunction(LLVMFuncOp func) {
13511351
llvmFunc->addFnAttr("no-signed-zeros-fp-math",
13521352
llvm::toStringRef(*noSignedZerosFpMath));
13531353

1354+
if (auto denormalFpMath = func.getDenormalFpMath())
1355+
llvmFunc->addFnAttr("denormal-fp-math",
1356+
*denormalFpMath);
1357+
1358+
if (auto denormalFpMathF32 = func.getDenormalFpMathF32())
1359+
llvmFunc->addFnAttr("denormal-fp-math-f32",
1360+
*denormalFpMathF32);
1361+
1362+
if (auto fpContract = func.getFpContract())
1363+
llvmFunc->addFnAttr("fp-contract",
1364+
*fpContract);
1365+
13541366
// Add function attribute frame-pointer, if found.
13551367
if (FramePointerKindAttr attr = func.getFramePointerAttr())
13561368
llvmFunc->addFnAttr("frame-pointer",

mlir/test/Dialect/LLVMIR/func.mlir

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,25 @@ module {
293293
// CHECK-SAME: attributes {convergent}
294294
llvm.return
295295
}
296+
297+
llvm.func @denormal_fp_math_roundtrip() attributes {denormal_fp_math = "preserve-sign"} {
298+
// CHECK: @denormal_fp_math_roundtrip
299+
// CHECK-SAME: attributes {denormal_fp_math = "preserve-sign"}
300+
llvm.return
301+
}
302+
303+
llvm.func @denormal_fp_math_f32_roundtrip() attributes {denormal_fp_math_f32 = "preserve-sign"} {
304+
// CHECK: @denormal_fp_math_f32_roundtrip
305+
// CHECK-SAME: attributes {denormal_fp_math_f32 = "preserve-sign"}
306+
llvm.return
307+
}
308+
309+
llvm.func @fp_contract_roundtrip() attributes {fp_contract = "fast"} {
310+
// CHECK: @fp_contract_roundtrip
311+
// CHECK-SAME: attributes {fp_contract = "fast"}
312+
llvm.return
313+
}
314+
296315
}
297316

298317
// -----

mlir/test/Target/LLVMIR/Import/function-attributes.ll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,24 @@ declare void @func_attr_no_signed_zeros_fp_math_true() "no-signed-zeros-fp-math"
344344
; CHECK-SAME: attributes {no_signed_zeros_fp_math = false}
345345
declare void @func_attr_no_signed_zeros_fp_math_false() "no-signed-zeros-fp-math"="false"
346346

347+
; // -----
348+
349+
; CHECK-LABEL: @func_attr_denormal_fp_math_ieee
350+
; CHECK-SAME: attributes {denormal_fp_math = "ieee"}
351+
declare void @func_attr_denormal_fp_math_ieee() "denormal-fp-math"="ieee"
352+
353+
; // -----
354+
355+
; CHECK-LABEL: @func_attr_denormal_fp_math_f32_preserve_sign
356+
; CHECK-SAME: attributes {denormal_fp_math_f32 = "preserve-sign"}
357+
declare void @func_attr_denormal_fp_math_f32_preserve_sign() "denormal-fp-math-f32"="preserve-sign"
358+
359+
; // -----
360+
361+
; CHECK-LABEL: @func_attr_fp_contract_fast
362+
; CHECK-SAME: attributes {fp_contract = "fast"}
363+
declare void @func_attr_fp_contract_fast() "fp-contract"="fast"
364+
347365
// -----
348366

349367
; CHECK-LABEL: @noinline_attribute

mlir/test/Target/LLVMIR/fp-math-function-attributes.mlir

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,30 @@ llvm.func @no_signed_zeros_fp_math_func_false() attributes {no_signed_zeros_fp_m
8787
llvm.return
8888
}
8989
// CHECK: attributes #[[ATTRS]] = { "no-signed-zeros-fp-math"="false" }
90+
91+
// -----
92+
93+
// CHECK-LABEL: define void @denormal_fp_math_func_ieee()
94+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
95+
llvm.func @denormal_fp_math_func_ieee() attributes {denormal_fp_math = "ieee"} {
96+
llvm.return
97+
}
98+
// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math"="ieee" }
99+
100+
// -----
101+
102+
// CHECK-LABEL: define void @denormal_fp_math_f32_func_preserve_sign()
103+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
104+
llvm.func @denormal_fp_math_f32_func_preserve_sign() attributes {denormal_fp_math_f32 = "preserve-sign"} {
105+
llvm.return
106+
}
107+
// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math-f32"="preserve-sign" }
108+
109+
// -----
110+
111+
// CHECK-LABEL: define void @fp_contract_func_fast()
112+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
113+
llvm.func @fp_contract_func_fast() attributes {fp_contract = "fast"} {
114+
llvm.return
115+
}
116+
// CHECK: attributes #[[ATTRS]] = { "fp-contract"="fast" }

0 commit comments

Comments
 (0)