This is an archive of the discontinued LLVM Phabricator instance.

[MC] Omit fill value if it's zero when emitting code alignment
ClosedPublic

Authored by steplong on Aug 23 2022, 3:17 PM.

Details

Summary

Previously, we were generating zeroes when generating code alignments for AArch64, but now we should omit the value and let the assembler choose to generate nops or zeroes.

Diff Detail

Event Timeline

steplong created this revision.Aug 23 2022, 3:17 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 23 2022, 3:17 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
steplong requested review of this revision.Aug 23 2022, 3:17 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 23 2022, 3:17 PM
steplong edited the summary of this revision. (Show Details)Aug 23 2022, 3:17 PM

Reinterpreting Value == 0 to mean the user didn't specify a value is wrong. We should distinguish between "no fill specified" and "fill zero specified". (At least, that's how it should work, as far as I can tell.)

In the "no fill specified" case, we should end up in emitCodeAlignment; we can emit the correct directive from there, instead of throwing away the information.

Reinterpreting Value == 0 to mean the user didn't specify a value is wrong. We should distinguish between "no fill specified" and "fill zero specified". (At least, that's how it should work, as far as I can tell.)

In the "no fill specified" case, we should end up in emitCodeAlignment; we can emit the correct directive from there, instead of throwing away the information.

That makes sense. Do you know how I can get the min byte width of the Value? The nop value for ARM64 is 0xd503201f which is 4 bytes, but X86's nop value is 0x90 which is 1 byte. Currently emitCodeAlignment looks like:

void emitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
                          unsigned ValueSize = 1,
                          unsigned MaxBytesToEmit = 0) override;

void MCAsmStreamer::emitCodeAlignment(unsigned ByteAlignment,
                                      const MCSubtargetInfo *STI,
                                      unsigned MaxBytesToEmit) {
  // Emit with a text fill value.
  emitValueToAlignment(ByteAlignment, MAI->getTextAlignFillValue(),
                       1, MaxBytesToEmit);
}

ValueSize is hardcoded to 1, but for ARM64 we would have to pass 4.

steplong updated this revision to Diff 455245.Aug 24 2022, 8:58 AM
  • Update text fill value to nop instruction for AArch64
  • Still need to set ValueSize arg to 4 for AArch64
steplong retitled this revision from [MC] Let assembler decide the fill value for p2align instead of 0x0 to [MC][AArch64] Output nops when aligning code for AArch64.Aug 24 2022, 8:59 AM
steplong edited the summary of this revision. (Show Details)
steplong updated this revision to Diff 455307.Aug 24 2022, 11:36 AM
steplong edited the summary of this revision. (Show Details)
  • Omit value if value is 0 when emitting code alignment
steplong retitled this revision from [MC][AArch64] Output nops when aligning code for AArch64 to [MC] Omit fill value if it's zero when emitting code alignment.Aug 24 2022, 11:41 AM
steplong edited the summary of this revision. (Show Details)
efriedma added inline comments.Aug 24 2022, 11:53 AM
llvm/lib/MC/MCAsmStreamer.cpp
248

The name "emitValueToAlignmentIfNotOmit" doesn't make any sense; the "IfNotOmit" part isn't related to the rest of the name. Maybe just "emitAlignmentDirective"?

251

The logic around OmitValueIfZero is sort of hard to understand. Maybe pass in an Optional<int64_t> Value, and make the caller figure out whether the value should be omitted?

steplong updated this revision to Diff 455333.Aug 24 2022, 12:41 PM
  • Change logic to use Optional
  • Rename to emitAlignmentDirective
efriedma added inline comments.Aug 24 2022, 1:26 PM
llvm/lib/MC/MCAsmStreamer.cpp
1463

else if (MaxBytesToEmit)?

1473

If I'm understand correctly, this is actually a visible change to emitValueToAlignment: it will always explicitly emit the value, even if it's zero. That's probably what we want (reducing ambiguity in our asm output seems like an improvement), but we should have test coverage for the change.

steplong updated this revision to Diff 455369.Aug 24 2022, 2:03 PM
  • Add test for .p2align NUM, 0x0 case
  • Add missing if check for MaxBytesToEmit
This revision is now accepted and ready to land.Aug 24 2022, 2:06 PM
MaskRay accepted this revision.Aug 25 2022, 12:03 AM

LGTM.

llvm/test/CodeGen/AArch64/p2align-zero-fillvalue.ll
1

Use aarch64 to test generic ELF behavior, not just linux-gnu.

8

Add trailing {{$}}

steplong updated this revision to Diff 455571.Aug 25 2022, 6:54 AM
  • Fix failing tests
  • Fixup p2align-zero-fillvalue.ll test
steplong updated this revision to Diff 455604.Aug 25 2022, 8:45 AM
  • Whoops missed a failing test