This is an archive of the discontinued LLVM Phabricator instance.

Allow 32-bit pointers to be written in 64-bit slots
Needs RevisionPublic

Authored by dmlloyd on Feb 8 2023, 1:09 PM.

Details

Reviewers
arsenm
smeenai
Summary

Pointer lowering will presently happily allow the assembler to truncate a pointer in order to fit it into a narrower slot. The assembler is also capable of extending a pointer into a larger slot, and this is needed for languages like Java which may initialize a 64-bit integer field with a 32-bit pointer value on the initial heap.

This change builds on D61325 to allow the assembler to also extend pointers into larger integer slots.

Fixes #60453.

Diff Detail

Event Timeline

dmlloyd created this revision.Feb 8 2023, 1:09 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 8 2023, 1:09 PM
dmlloyd requested review of this revision.Feb 8 2023, 1:09 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 8 2023, 1:09 PM

I don't understand the build failure. clang-format was seemingly happy with the input, and I was able to build locally.

arsenm added inline comments.Feb 8 2023, 2:13 PM
test/CodeGen/WebAssembly/ptrtoint-widen.ll
4 ↗(On Diff #495929)

Can you include some tests with different sized result integers? I thought that was the point of the patch

dmlloyd added inline comments.Feb 8 2023, 2:19 PM
test/CodeGen/WebAssembly/ptrtoint-widen.ll
4 ↗(On Diff #495929)

For these two targets, the native pointer size is 32 bits, so i64 *is* the different-sized result. But I can also add in the native size and a shorter one for good measure. I will look around other tests to figure out how the CHECK would work in those cases.

dmlloyd added inline comments.Feb 8 2023, 2:25 PM
test/CodeGen/WebAssembly/ptrtoint-widen.ll
4 ↗(On Diff #495929)

Well, it looks like there already is a test for narrowing a 64-bit pointer to 32 bits (llvm/test/CodeGen/X86/ptrtoint-narrow.ll for example), and plain ptrtoint is tested by other tests. But what I didn't realize before was that there is a test that specifically tests the inverse to the behavior I just changed, so I'll remove that and update the diff.

dmlloyd updated this revision to Diff 495955.Feb 8 2023, 2:28 PM

Updated to remove a conflicting test.

dmlloyd updated this revision to Diff 496123.Feb 9 2023, 7:27 AM

Another attempt to quell the build failures that I cannot reproduce locally.

dmlloyd updated this revision to Diff 496152.Feb 9 2023, 9:10 AM

The CI system apparently does not like git diffs with --no-prefix.

I think all concerns are now addressed and the build seems happy now. Did my explanation of the test situation make sense @arsenm?

arsenm added inline comments.Feb 9 2023, 2:37 PM
llvm/test/CodeGen/X86/ptrtoint-widen.ll
5

I still think it’s worthwhile to have a variety of destination integer sizes in the same test

dmlloyd updated this revision to Diff 496254.Feb 9 2023, 3:16 PM

Updated to test more integer sizes per @arsenm.

I'm concerned that this is going to emit expressions that the assembler can't actually handle. ".quad a" gives an error on many targets, including ARM and 32-bit x86.

".quad a" gives an error on many targets, including ARM and 32-bit x86.

My first test case was 32-bit x86 so I can confirm that this works, however you are otherwise correct. I did some detailed manual testing and in addition to 64-bit platforms (obviously), it also works on these (presumably) non-64-bit targets, which all appear to have syntax for writing a 64-bit value (I am not familiar with all of these so forgive me for any errors):

  • aarch64_32
  • avr
  • i686
  • loongarch32
  • mips & mipsel
  • riscv32
  • wasm32

However it does fail with "Don't know how to emit this value" on these platforms:

  • arm & armeb
  • ppc32 & ppc32le
  • sparc & sparcel
  • thumb & thumbeb

Testing with plain integers in these cases reveals that they normally emit a 64-bit constant as a pair of 32-bit words (usually .long).

So I'm thinking that there is some logic which knows to lower i64 into two distinct values in endian-correct order. Thus my plan is now to locate this information and figure out a way to use it on these platforms to split and zero-extend the lowered constant appropriately.

An alternative plan would be to error as before on these platforms. It's trading one error for another but it'd still be an improvement from my perspective at least.

Not sure how you're building i686, but I definitely get an error. (https://godbolt.org/z/MW3x3WKrr)

On the targets where it's working, I see 64-bit relocations (R_WASM_MEMORY_ADDR_I64, R_RISCV_64, R_MIPS_64), which might work with some linkers, but maybe not all.

I'd prefer to just explicitly pad values in the AsmPrinter, and avoid depending on obscure assembler behaviors.

arsenm added a comment.Jul 7 2023, 4:40 AM

I'd prefer to just explicitly pad values in the AsmPrinter, and avoid depending on obscure assembler behaviors.

Is that really worse than having the backend error?

We have the following options here:

  1. Leave it as, i.e. a backend error
  2. Commit this as-is, i.e. depending on the target, it might work, but it could also either crash in the assembler or generate an invalid relocation.
  3. Teach the assembler to emit padding for .quad.
  4. Emit explicit padding in the AsmPrinter

(2) seems like the worst of these options.

We have the following options here:

  1. Leave it as, i.e. a backend error
  2. Commit this as-is, i.e. depending on the target, it might work, but it could also either crash in the assembler or generate an invalid relocation.
  3. Teach the assembler to emit padding for .quad.
  4. Emit explicit padding in the AsmPrinter

(2) seems like the worst of these options.

As the original author, I agree; when I wrote this I had wrongly assumed that all assemblers already supported this. I wouldn't mind if (3) or (4) were done but this would likely be beyond my experience with C++.

arsenm requested changes to this revision.Jul 26 2023, 2:22 PM

moving out of needs review given there's an apparent desire to do something else here

This revision now requires changes to proceed.Jul 26 2023, 2:22 PM
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp