This is an archive of the discontinued LLVM Phabricator instance.

[mlir][Arith] Add pass for emulating unsupported float ops (#1079)
ClosedPublic

Authored by krzysz00 on Jul 5 2023, 12:09 PM.

Details

Summary

To complement the bf16 expansion and truncation patterns added to
ExpandOps, define a pass that replaces, for any arithmetic operation
op,
%y = arith.op %v0, %v1, ... : T
with
%e0 = arith.expf %v0 : T to U
%e1 = arith.expf %v1 : T to U
...
%y.exp = arith.op %e0, %e1, ... : U
%y = arith.truncf %y.exp : U to T

This allows for "emulating" floating-point operations not supported on
a given target (such as bfloat operations or most arithmetic on 8-bit
floats) by extending those types to supported ones, performing the
arithmetic operation, and then truncating back to the original
type (which ensures appropriate rounding behavior).

The lowering of the extf and truncf ops introduced by this
transformation should be handled by subsequent passes.

Diff Detail

Event Timeline

krzysz00 created this revision.Jul 5 2023, 12:09 PM
Herald added a project: Restricted Project. · View Herald Transcript
krzysz00 requested review of this revision.Jul 5 2023, 12:09 PM
rsuderman added inline comments.Jul 7 2023, 2:37 PM
mlir/test/Dialect/Arith/emulate-unsupported-floats.mlir
3

Could you include a test where we do not need to emulate? This verifies that the other types are left alone.

krzysz00 updated this revision to Diff 538795.Jul 10 2023, 1:27 PM

Add test for checking that other types are unaffected by this pass.

rsuderman accepted this revision.Jul 10 2023, 3:04 PM
This revision is now accepted and ready to land.Jul 10 2023, 3:04 PM

Because assertions compile to empty when built in opt mode, EmulateUnsupportedFloats.cpp:82 had an unused variable. So I propagated that unused variable into the assert in 5671f023042b558d38c3b777ee4ae0ad037b1867.

That fix was erroneous.

It turns out that the assert was doing real work, which gets compiled to nothing in opt mode, and makes some tests fail. eg, llvm-project/mlir/test/Dialect:Arith/emulate-unsupported-floats.mlir.test

I tried to fix this by adjusting the assert, but I get a raft of other compilation errors. So I have removed the assert altogether in 5f4d96ebef2387b1acffe3b6173a7fd625be0930. You will likely want to add it back, but for now all the tests pass.

mlir/test/Dialect/Arith/emulate-unsupported-floats.mlir
3

ter

Oh. That's ... whoops. I hadn't realized I put actual work in an assert.