This is an archive of the discontinued LLVM Phabricator instance.

[GISel] Allow ConstantFoldBinOp to consider G_FCONSTANT binary representation for combines
ClosedPublic

Authored by kariddi on Oct 9 2019, 3:22 PM.

Details

Summary

In GlobalISel there's no different type representing Floats or Ints , but just bag of bits types (s16,s32,s64...).

When IRTranslator generates code like this:

%v = bitcast float 1.0 to i32
%v2 = and i32 %x, %v

It will translate the code to

%0_(s32) = G_FCONSTANT float 1.0
%1_(s32) = G_AND %X(s32), %0(s32)

Because a G_BITCAST from s32 to s32 doesn't make sense in this case ...

So from this behavior its clear that IRTranslator considers the output of "G_FCONSTANT" just a bag of bits the origin of which was a float constant.

Currently though in the constant folder we don't consider constants coming from G_FCONSTANTs for folding purposes as the folder considers them "float data" .
In reality though we should consider them like the "untyped bit representation"of a float and it should participate in any constant folding involving "integer" data.

Diff Detail

Repository
rL LLVM

Event Timeline

kariddi created this revision.Oct 9 2019, 3:22 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 9 2019, 3:22 PM
arsenm added a subscriber: arsenm.Oct 9 2019, 3:41 PM
arsenm added inline comments.
llvm/lib/CodeGen/GlobalISel/Utils.cpp
323–325

Why do you need to whitelist these types?

kariddi marked an inline comment as done.Oct 9 2019, 3:46 PM
kariddi added inline comments.
llvm/lib/CodeGen/GlobalISel/Utils.cpp
323–325

Are the types that fit a uint64 that we know of, but I guess that could be checked from "the only user" of this function instead

kariddi marked an inline comment as done.Oct 9 2019, 3:50 PM
kariddi added inline comments.
llvm/lib/CodeGen/GlobalISel/Utils.cpp
323–325

Actually, that's not necessarily true ... ConstantFoldBinOp used to use a function that only handled Optional<int64_t> . Now I substituted that with a function that returns APInt (for simplicity), but I wanted to keep the functionality the same as getConstantVRegVal() I guess ...

I guess I can remove that limitation for the Floats, but maintain the limitation fo the integers and then check in ConstantFoldBinOp.

I'm just scared that allowing ConstantFoldBinOp to digest things it never saw before would cause some "unexpected consequence" :-P

arsenm added inline comments.Oct 9 2019, 3:58 PM
llvm/lib/CodeGen/GlobalISel/Utils.cpp
323–325

I'm worried about somebody adding bfloat16 or something and then never updating this list. I think it would be fine to just return anything bitcastToAPInt will handle

kariddi marked 2 inline comments as done.Oct 9 2019, 5:06 PM
kariddi added inline comments.
llvm/lib/CodeGen/GlobalISel/Utils.cpp
323–325

I tried removing the limitation. It seems not not cause problems , so I'll update the patch

kariddi updated this revision to Diff 224211.Oct 9 2019, 5:07 PM
kariddi marked an inline comment as done.

Clang-formatted test and corrected "MRI" to "*MRI"

Removed limitation pointed out by arsenm

arsenm added inline comments.Oct 9 2019, 5:45 PM
llvm/lib/CodeGen/GlobalISel/Utils.cpp
321–324

I kind of don't like potentially repeating the search through copy/extensions twice. Can you maybe add some kind of parameter to getConstantVRegValWithLookThrough, so it handles both?

kariddi updated this revision to Diff 224441.Oct 10 2019, 12:19 PM

So, I changed the getConstantVRegValWithLookThrough function with an extra operand. Honestly I couldn't see any reason why the new parameter should have been set as "false" by default, so I set it to true, because it seems what the design of GlobalISel seems to suggest considering the difference of float/int being "not-there".

This allowed me to remove the getAnyConstantVRegVal() function and use the normal getConstantVRegVal()

Instead. A test in AMDGPU started to have different output from what I understand because of the new folding and I updated it

arsenm accepted this revision.Oct 10 2019, 2:19 PM

LGTM. I'm not sure we even really need G_FCONSTANT as an instruction

llvm/lib/CodeGen/GlobalISel/Utils.cpp
240–241

No else after return

This revision is now accepted and ready to land.Oct 10 2019, 2:19 PM
This revision was automatically updated to reflect the committed changes.