Skip to content

Commit 6938bf8

Browse files
committed
[MLIR][LLVM] Add ftz and fuse FP ops related function attribute support
1 parent d64efe4 commit 6938bf8

File tree

6 files changed

+214
-0
lines changed

6 files changed

+214
-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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,72 @@ 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_preserve_sign
356+
; CHECK-SAME: attributes {denormal_fp_math = "preserve-sign"}
357+
declare void @func_attr_denormal_fp_math_preserve_sign() "denormal-fp-math"="preserve-sign"
358+
359+
; // -----
360+
361+
; CHECK-LABEL: @func_attr_denormal_fp_math_positive_zero
362+
; CHECK-SAME: attributes {denormal_fp_math = "positive-zero"}
363+
declare void @func_attr_denormal_fp_math_positive_zero() "denormal-fp-math"="positive-zero"
364+
365+
; // -----
366+
367+
; CHECK-LABEL: @func_attr_denormal_fp_math_dynamic
368+
; CHECK-SAME: attributes {denormal_fp_math = "dynamic"}
369+
declare void @func_attr_denormal_fp_math_dynamic() "denormal-fp-math"="dynamic"
370+
371+
; // -----
372+
373+
; CHECK-LABEL: @func_attr_denormal_fp_math_f32_ieee
374+
; CHECK-SAME: attributes {denormal_fp_math_f32 = "ieee"}
375+
declare void @func_attr_denormal_fp_math_f32_ieee() "denormal-fp-math-f32"="ieee"
376+
377+
; // -----
378+
379+
; CHECK-LABEL: @func_attr_denormal_fp_math_f32_preserve_sign
380+
; CHECK-SAME: attributes {denormal_fp_math_f32 = "preserve-sign"}
381+
declare void @func_attr_denormal_fp_math_f32_preserve_sign() "denormal-fp-math-f32"="preserve-sign"
382+
383+
; // -----
384+
385+
; CHECK-LABEL: @func_attr_denormal_fp_math_f32_positive_zero
386+
; CHECK-SAME: attributes {denormal_fp_math_f32 = "positive-zero"}
387+
declare void @func_attr_denormal_fp_math_f32_positive_zero() "denormal-fp-math-f32"="positive-zero"
388+
389+
; // -----
390+
391+
; CHECK-LABEL: @func_attr_denormal_fp_math_f32_dynamic
392+
; CHECK-SAME: attributes {denormal_fp_math_f32 = "dynamic"}
393+
declare void @func_attr_denormal_fp_math_f32_dynamic() "denormal-fp-math-f32"="dynamic"
394+
395+
; // -----
396+
397+
; CHECK-LABEL: @func_attr_fp_contract_fast
398+
; CHECK-SAME: attributes {fp_contract = "fast"}
399+
declare void @func_attr_fp_contract_fast() "fp-contract"="fast"
400+
401+
; // -----
402+
403+
; CHECK-LABEL: @func_attr_fp_contract_on
404+
; CHECK-SAME: attributes {fp_contract = "on"}
405+
declare void @func_attr_fp_contract_on() "fp-contract"="on"
406+
407+
; // -----
408+
409+
; CHECK-LABEL: @func_attr_fp_contract_off
410+
; CHECK-SAME: attributes {fp_contract = "off"}
411+
declare void @func_attr_fp_contract_off() "fp-contract"="off"
412+
347413
// -----
348414

349415
; CHECK-LABEL: @noinline_attribute

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,102 @@ 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_func_preserve_sign()
103+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
104+
llvm.func @denormal_fp_math_func_preserve_sign() attributes {denormal_fp_math = "preserve-sign"} {
105+
llvm.return
106+
}
107+
// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math"="preserve-sign" }
108+
109+
// -----
110+
111+
// CHECK-LABEL: define void @denormal_fp_math_func_positive_zero()
112+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
113+
llvm.func @denormal_fp_math_func_positive_zero() attributes {denormal_fp_math = "positive-zero"} {
114+
llvm.return
115+
}
116+
// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math"="positive-zero" }
117+
118+
// -----
119+
120+
// CHECK-LABEL: define void @denormal_fp_math_func_dynamic()
121+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
122+
llvm.func @denormal_fp_math_func_dynamic() attributes {denormal_fp_math = "dynamic"} {
123+
llvm.return
124+
}
125+
// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math"="dynamic" }
126+
127+
// -----
128+
129+
// CHECK-LABEL: define void @denormal_fp_math_f32_func_ieee()
130+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
131+
llvm.func @denormal_fp_math_f32_func_ieee() attributes {denormal_fp_math_f32 = "ieee"} {
132+
llvm.return
133+
}
134+
// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math-f32"="ieee" }
135+
136+
// -----
137+
138+
// CHECK-LABEL: define void @denormal_fp_math_f32_func_preserve_sign()
139+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
140+
llvm.func @denormal_fp_math_f32_func_preserve_sign() attributes {denormal_fp_math_f32 = "preserve-sign"} {
141+
llvm.return
142+
}
143+
// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math-f32"="preserve-sign" }
144+
145+
// -----
146+
147+
// CHECK-LABEL: define void @denormal_fp_math_f32_func_positive_zero()
148+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
149+
llvm.func @denormal_fp_math_f32_func_positive_zero() attributes {denormal_fp_math_f32 = "positive-zero"} {
150+
llvm.return
151+
}
152+
// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math-f32"="positive-zero" }
153+
154+
// -----
155+
156+
// CHECK-LABEL: define void @denormal_fp_math_f32_func_dynamic()
157+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
158+
llvm.func @denormal_fp_math_f32_func_dynamic() attributes {denormal_fp_math_f32 = "dynamic"} {
159+
llvm.return
160+
}
161+
// CHECK: attributes #[[ATTRS]] = { "denormal-fp-math-f32"="dynamic" }
162+
163+
// -----
164+
165+
// CHECK-LABEL: define void @fp_contract_func_fast()
166+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
167+
llvm.func @fp_contract_func_fast() attributes {fp_contract = "fast"} {
168+
llvm.return
169+
}
170+
// CHECK: attributes #[[ATTRS]] = { "fp-contract"="fast" }
171+
172+
// -----
173+
174+
// CHECK-LABEL: define void @fp_contract_func_on()
175+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
176+
llvm.func @fp_contract_func_on() attributes {fp_contract = "on"} {
177+
llvm.return
178+
}
179+
// CHECK: attributes #[[ATTRS]] = { "fp-contract"="on" }
180+
181+
// -----
182+
183+
// CHECK-LABEL: define void @fp_contract_func_off()
184+
// CHECK-SAME: #[[ATTRS:[0-9]+]]
185+
llvm.func @fp_contract_func_off() attributes {fp_contract = "off"} {
186+
llvm.return
187+
}
188+
// CHECK: attributes #[[ATTRS]] = { "fp-contract"="off" }

0 commit comments

Comments
 (0)