This is an archive of the discontinued LLVM Phabricator instance.

[MC] Fold A-B when A's fragment precedes B's fragment
ClosedPublic

Authored by MaskRay on Jun 15 2023, 8:50 PM.

Details

Summary

When the MCAssembler is non-null and the MCAsmLayout is null, we can fold A-B
when

  • A and B are in the same fragment, or
  • A's fragment suceeds B's fragment, and they are not separated by non-data fragments (D69411)

This patch allows folding when A's fragment precedes B's fragment so
that 9997b - . == 0 below can be evaluated as true:

nop
.arch_extension sec
9997:nop
// old behavior: error: expected absolute expression
.if 9997b - . == 0
.endif

Add a case to llvm/test/MC/ARM/directive-if-subtraction.s.
Note: for MCAsmStreamer, we cannot evaluate .if . - 9997b == 0 at parse
time due to MCAsmStreamer::getAssemblerPtr returning nullptr (D45164).

Some Darwin tests check that this folding does not work. Add .p2align 2 to
block some label difference folding or adjust the tests.

Diff Detail

Event Timeline

MaskRay created this revision.Jun 15 2023, 8:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2023, 8:50 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
MaskRay requested review of this revision.Jun 15 2023, 8:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2023, 8:50 PM

The logic below is quite difficult to follow. Displacement being negated twice on some code paths is very confusing.
Can it be simplified somehow? Possibly by more intrusive changes?
Otherwise LGTM.

The logic below is quite difficult to follow. Displacement being negated twice on some code paths is very confusing.
Can it be simplified somehow? Possibly by more intrusive changes?
Otherwise LGTM.

Thanks for sharing thoughts. I have locally implemented another approach that detects which of FA/FB occurs first, but the code turns out to be more complex, and more complex when the RISC-V linker relaxation code (D153097) is added...
The current code is the best I can think of.

llvm/lib/MC/MCExpr.cpp
667–672

is there a more concise way to express this with std::find?

693–698

typo: s/dummay/dummy/

llvm/test/MC/AArch64/arm64-small-data-fixups.s
5

let's keep the indentation of assembler directives consistent

llvm/test/MC/ARM/directive-if-subtraction.s
22

I thought the whole point of this patch is to allow 9997b - . == 0 to fold? Then why is this testing for that to emit an error?

llvm/test/MC/MachO/reloc-diff.s
4

indent

MaskRay updated this revision to Diff 533122.Jun 20 2023, 9:05 PM
MaskRay marked 4 inline comments as done.
MaskRay edited the summary of this revision. (Show Details)

improve tests
simplify for (auto FI = FA->getIterator(), FE = SecA.end(); ++FI != FE;) { with a std::find.

MaskRay added inline comments.Jun 20 2023, 9:05 PM
llvm/test/MC/ARM/directive-if-subtraction.s
22

This test complements the previous test that we still have // ASM:[[@LINE-2]]:5: error: expected absolute expression.

This is related to a larger issue that for now we use MCAsmStreamer::getAssemblerPtr that returns null (related to D45164). I have found that improving this would be challenging.

MaskRay marked an inline comment as done.Jun 20 2023, 9:06 PM
nickdesaulniers accepted this revision.Jun 22 2023, 11:50 AM

Please fix the last indentation.

llvm/test/MC/ARM/directive-if-subtraction.s
22

I see; the run lines are super perplexing to me; changing the output type from obj (to whatever the implicit default is, another .s file perhaps?) causes the assembler to error? WEIRD

llvm/test/MC/MachO/reloc-diff.s
4

This was marked done but does not look fixed to me, please triple check. Later directives are indented. Please indent this one to be consistent.

This revision is now accepted and ready to land.Jun 22 2023, 11:50 AM
MaskRay updated this revision to Diff 533724.Jun 22 2023, 12:19 PM
MaskRay marked an inline comment as done.
MaskRay edited the summary of this revision. (Show Details)

indent.
Thank you for the review! Adjusting the description slightly...

llvm/test/MC/ARM/directive-if-subtraction.s
22

Ah, this patch adds a label difference test that serves as a dual purpose:

  • -filetype=obj now folds if 9997b - . == 0
  • -filetype=asm cannot fold such expressions

The current summary

Add a case to llvm/test/MC/ARM/directive-if-subtraction.s that we don't evaluate .if . - 9997b == 0 for MCAsmStreamer due to getAssemblerPtr returns nullptr.

llvm/test/MC/MachO/reloc-diff.s
4

Sorry, missed this one. I'll indent it.

(I considered .p2align 2 similar to the function label, as some directives guarding the following body, so I did not indent it. But I can see an argument for just indenting every align directives.

I think this might be a minor issue of the traditional compiler-generated directives. For example, not indenting .globl probably would improve readability.
)

This revision was landed with ongoing or failed builds.Jun 22 2023, 12:24 PM
This revision was automatically updated to reflect the committed changes.
chapuni added inline comments.
llvm/lib/MC/MCExpr.cpp
696

Found has become unused in -Asserts.