This is an archive of the discontinued LLVM Phabricator instance.

[GlobalISel] Add G_BFI (bitfield insertion opcode)
Needs ReviewPublic

Authored by kschwarz on Oct 25 2021, 4:09 AM.

Details

Summary

Similar to G_[SU]BFX, many targets support similar bitfield insertion operations.

The most generic form extracts a number of bits (width) from a register (val), starting at bit 0.
Those bits are shifted by a certain shift amount (pos), and inserted into another register (src).
The resulting value is written into the output register:

%x = G_BFI %src, %val, %pos, %width

There are several patterns that can be combined to a generic bitfield insertion operation.

Diff Detail

Event Timeline

kschwarz created this revision.Oct 25 2021, 4:09 AM
kschwarz requested review of this revision.Oct 25 2021, 4:09 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 25 2021, 4:09 AM
Herald added a subscriber: wdng. · View Herald Transcript
foad added inline comments.Oct 25 2021, 4:19 AM
llvm/include/llvm/Target/GenericOpcodes.td
1422

Can you add a comment like for sbfx and ubfx, and explain the limits on pos and witdth?

llvm/lib/CodeGen/MachineVerifier.cpp
1617

Any reason why it's not supported?

kschwarz updated this revision to Diff 381998.Oct 25 2021, 7:52 AM

Add comment about range of operands

Thanks for the quick review @foad!

llvm/lib/CodeGen/MachineVerifier.cpp
1617

Not really. I wanted to focus on scalars first, and basically took this from the bitfield extracts above.

foad added a comment.Oct 25 2021, 8:12 AM

Looks OK to me technically. I don't have an opinion on the big question of whether it's a good idea to introduce this opcode.

paquette added inline comments.Oct 25 2021, 10:04 AM
llvm/lib/CodeGen/MachineVerifier.cpp
1616

Maybe add a FIXME or TODO here noting that it may make sense to extend this to vectors in the future? (I can't think of any use-cases, but maybe it makes sense on some target...)

llvm/test/MachineVerifier/test_g_bfi.mir
13

Some tests that would be good to add:

Test that the verifier complains when...

  • Type index 0 != type index 1
  • Type index 1 != type index 2
  • Type index 3 != type index 4

e.g.

%x:_(s32) = G_BFI %y:_(s64), ...

should complain

arsenm added inline comments.Oct 25 2021, 10:27 AM
llvm/lib/CodeGen/MachineVerifier.cpp
1616

Supporting on vectors is good for consistency and also can help with combine ordering problems

arsenm added inline comments.Oct 25 2021, 10:29 AM
llvm/lib/CodeGen/MachineVerifier.cpp
1616

I don't think this should disallow vectors. Having the legalizer scalarize is trivial

I think it's a better question if the second/third operands can be vectors or not

I'm fine with allowing vectors, but shouldn't we then also allow vectors for the G_*BFX? What was the reason for disallowing them there?

I'm fine with allowing vectors, but shouldn't we then also allow vectors for the G_*BFX? What was the reason for disallowing them there?

Yes. I don't think any opcode should be arbitrarily restricted from vectors

Yeah I think we can drop the vector restriction on G_*BFX. I don't think there's any reason to disallow vectors now that I think about it.

foad added inline comments.Nov 30 2021, 2:08 AM
llvm/lib/CodeGen/MachineVerifier.cpp
1616

I think it's a better question if the second/third operands can be vectors or not

What are the rules for basic shifts like G_SHL ? I don't see any special handling for them in MachineVerifier. Do we really allow any combination of scalars and vectors for the first and second inputs?

arsenm added inline comments.Dec 15 2021, 4:51 PM
llvm/lib/CodeGen/MachineVerifier.cpp
1616

For basic shifts it's all operands vector or all scalar

foad added inline comments.Dec 16 2021, 2:41 AM
llvm/lib/CodeGen/MachineVerifier.cpp
1616
kschwarz added inline comments.Dec 21 2021, 2:10 AM
llvm/lib/CodeGen/MachineVerifier.cpp
1616

Having the width and pos operands support vectors too allows different values per lane, which is probably the most generic form.
Are you aware of any ISA that has such instructions?