This is an archive of the discontinued LLVM Phabricator instance.

[WebAssembly] Initial implementation of PIC code generation
ClosedPublic

Authored by sbc100 on Nov 16 2018, 1:28 PM.

Details

Summary

This change implements lowering of references global symbols in
PIC mode.

This change implements lowering of global references in PIC mode using
a new @GOT reference type. @GOT references can be used with function
or data symbol names combined with the get_global instruction. In
this case the linker will insert the wasm global that stores the address of
the symbol (either in memory for data symbols or in the wasm table
for function symbols).

For now I'm continuing to use the R_WASM_GLOBAL_INDEX_LEB relocation
type for this type of reference which means that this relocation type can
refer to either a global or a function or data symbol. We could choose to
introduce specific relocation types for GOT entries in the future.

See the current dynamic linking proposal:
https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md

Diff Detail

Repository
rLLD LLVM Linker

Event Timeline

sbc100 created this revision.Nov 16 2018, 1:28 PM
sbc100 edited the summary of this revision. (Show Details)Nov 16 2018, 1:48 PM
sbc100 added a reviewer: dschuff.

@dschuff can you take a look at the WebAssemblyFastISel.cpp part? I want to make sure I'm on the right track.

I'm currently looking at extending this to all the cases in test/CodeGen/WebAssembly/address-offsets.ll

sbc100 updated this revision to Diff 174481.Nov 16 2018, 4:36 PM

get parts of address-offsets.ll passing

aheejin added inline comments.Nov 16 2018, 4:55 PM
lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
94 ↗(On Diff #174481)

Delete this?

109 ↗(On Diff #174481)

Delete this too?

lib/Target/WebAssembly/WebAssemblyFastISel.cpp
799 ↗(On Diff #174481)

Delete?

lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
736 ↗(On Diff #174481)

Delete?

sbc100 marked 2 inline comments as done.Nov 26 2018, 10:17 AM
sbc100 added inline comments.
lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
94 ↗(On Diff #174481)

Sorry, this change is just WIP, not ready to commit yet. I'll remove this stuff before then

sbc100 updated this revision to Diff 190178.Mar 11 2019, 4:02 PM
sbc100 marked an inline comment as done.

rebase

Herald added a project: Restricted Project. · View Herald TranscriptMar 11 2019, 4:02 PM
sbc100 updated this revision to Diff 190179.Mar 11 2019, 4:04 PM
  • move to monorepo
sbc100 updated this revision to Diff 190709.Mar 14 2019, 12:46 PM

revert parts

sbc100 updated this revision to Diff 190731.Mar 14 2019, 2:39 PM
  • isel start
sbc100 updated this revision to Diff 190896.Mar 15 2019, 2:09 PM
  • selection dag working
sbc100 updated this revision to Diff 190903.Mar 15 2019, 2:38 PM
  • improve test
sbc100 updated this revision to Diff 190914.Mar 15 2019, 3:34 PM
  • cleaup test code
  • add MC test
sbc100 retitled this revision from WIP: [WebAssembly] First pass the implement -fPIC to [WebAssembly] Initial implementation of -fPIC codegeneration.Mar 15 2019, 3:35 PM
sbc100 edited the summary of this revision. (Show Details)
sbc100 added a reviewer: tlively.
tlively added inline comments.Mar 15 2019, 4:11 PM
llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h
94 ↗(On Diff #190914)

Is there a reason to use 1 << 3 instead of 0x8 to match the other enum values?

llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
422 ↗(On Diff #190914)

Should this be dependent on Subtarget->hasAddr64() like Opc is?

428 ↗(On Diff #190914)

Same question for OpcAdd.

476 ↗(On Diff #190914)

Should this commented code be deleted?

llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
765 ↗(On Diff #190914)

More commented code here.

llvm/test/MC/WebAssembly/reloc-pic.s
1 ↗(On Diff #190914)

If you want, you can avoid the temp file and output to stdout using -o -. I find that having the entire test on one line with no intermediate files makes the test easier to debug when something changes.

sbc100 updated this revision to Diff 190924.Mar 15 2019, 4:22 PM
sbc100 marked 7 inline comments as done.
  • feedback
  • cleanup
sbc100 updated this revision to Diff 190925.Mar 15 2019, 4:25 PM
  • clang-format
  • remove 64
sbc100 added inline comments.Mar 15 2019, 4:27 PM
llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
422 ↗(On Diff #190914)

Hmm , good question. I'm temped to not to add to much wasm64 code because we have no way of testing it and its premature .. so I think I'll just remove the ternary above.

llvm/test/MC/WebAssembly/reloc-pic.s
1 ↗(On Diff #190914)

Thanks (looks like i don't even need -o - as its the default.

tlively accepted this revision.Mar 15 2019, 4:32 PM
tlively added inline comments.
llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp
422 ↗(On Diff #190914)

sounds good to me!

This revision is now accepted and ready to land.Mar 15 2019, 4:32 PM
dschuff accepted this revision.Mar 15 2019, 5:03 PM
dschuff added inline comments.
llvm/include/llvm/MC/MCSymbolWasm.h
21 ↗(On Diff #190925)

Macro ohno:  mutable

sad how often mutable is used in LLVM. I guess maybe it means we're too aggressive about const overall?

wow that's a very large meme.

sbc100 marked an inline comment as done.Mar 15 2019, 6:25 PM
sbc100 added inline comments.
llvm/include/llvm/MC/MCSymbolWasm.h
21 ↗(On Diff #190925)

I only dun it cos they already dun it before guvnor: https://github.com/llvm-mirror/llvm/blob/7feefc2fe73c27062407322a14241d1a4497cdb1/include/llvm/MC/MCSymbol.h#L106

TBH I didn't even know about this keyword! And its seems crazy but it seems like that is the solution in this case.

sbc100 updated this revision to Diff 190940.Mar 15 2019, 7:51 PM
  • remove_lld_todo
aheejin added inline comments.Mar 17 2019, 12:42 AM
llvm/test/CodeGen/WebAssembly/address-offsets.ll
43 ↗(On Diff #190940)

Are these temporarily disabled? (Same for other occurrences below) If so, can we add a comment why? If it's not temporary we can maybe delete them?

sbc100 updated this revision to Diff 191185.Mar 18 2019, 2:59 PM
  • add more tests
sbc100 marked an inline comment as done.Mar 18 2019, 3:07 PM
sbc100 added inline comments.
llvm/test/CodeGen/WebAssembly/address-offsets.ll
43 ↗(On Diff #190940)

These tests we deliberately disabled in https://reviews.llvm.org/D24053. See the TODO comment above.

Whoever gets around to fixing them will need to make the fast-isel compatible too.

sbc100 updated this revision to Diff 191554.Mar 20 2019, 12:14 PM
  • cleanup test
  • revert fast isel changes for now
sbc100 updated this revision to Diff 191556.Mar 20 2019, 12:22 PM
  • revert fast isel changes
  • cleanup
sbc100 retitled this revision from [WebAssembly] Initial implementation of -fPIC codegeneration to [WebAssembly] Initial implementation of PIC code generation.Mar 20 2019, 12:27 PM
sbc100 edited the summary of this revision. (Show Details)
sbc100 updated this revision to Diff 191558.Mar 20 2019, 12:34 PM
sbc100 edited the summary of this revision. (Show Details)
  • cleanup
sbc100 updated this revision to Diff 191810.Mar 21 2019, 6:05 PM
  • add GOT.mem and GOT.func
sbc100 edited the summary of this revision. (Show Details)Mar 21 2019, 6:13 PM
sbc100 edited the summary of this revision. (Show Details)
sbc100 updated this revision to Diff 192219.Mar 25 2019, 3:48 PM
sbc100 edited the summary of this revision. (Show Details)
  • cleanup
sbc100 edited the summary of this revision. (Show Details)Mar 25 2019, 3:54 PM
dschuff accepted this revision.Mar 25 2019, 5:14 PM

still LGTM. In a followup CL it should be straightforward to add back fast-isel and you might be able to re-use the same checks in load-store-pic.ll. (but you wouldn't need to add fast-isel+PIC to address-offsets.ll because that's testing an optimization that fast-isel doesn't do.)

This revision was automatically updated to reflect the committed changes.

This broke wasm2.test_openjpeg on CI, reproducible locally. Errors on error: undefined symbol: __memory_base