Skip to content

Commit fa750f0

Browse files
committed
Revert "[MC] Remove UseAssemblerInfoForParsing"
This reverts commit 03c53c6. This causes very large compile-time regressions in some cases, e.g. sqlite3 at O0 regresses by 5%.
1 parent 26fabdd commit fa750f0

File tree

10 files changed

+40
-16
lines changed

10 files changed

+40
-16
lines changed

clang/tools/driver/cc1as_main.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,9 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
576576
Str.get()->emitZeros(1);
577577
}
578578

579+
// Assembly to object compilation should leverage assembly info.
580+
Str->setUseAssemblerInfoForParsing(true);
581+
579582
bool Failed = false;
580583

581584
std::unique_ptr<MCAsmParser> Parser(

llvm/include/llvm/MC/MCStreamer.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ class MCStreamer {
245245
/// requires.
246246
unsigned NextWinCFIID = 0;
247247

248+
bool UseAssemblerInfoForParsing;
249+
248250
/// Is the assembler allowed to insert padding automatically? For
249251
/// correctness reasons, we sometimes need to ensure instructions aren't
250252
/// separated in unexpected ways. At the moment, this feature is only
@@ -294,10 +296,11 @@ class MCStreamer {
294296

295297
MCContext &getContext() const { return Context; }
296298

297-
// MCObjectStreamer has an MCAssembler and allows more expression folding at
298-
// parse time.
299299
virtual MCAssembler *getAssemblerPtr() { return nullptr; }
300300

301+
void setUseAssemblerInfoForParsing(bool v) { UseAssemblerInfoForParsing = v; }
302+
bool getUseAssemblerInfoForParsing() { return UseAssemblerInfoForParsing; }
303+
301304
MCTargetStreamer *getTargetStreamer() {
302305
return TargetStreamer.get();
303306
}

llvm/lib/CodeGen/AsmPrinter/AsmPrinterInlineAsm.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ void AsmPrinter::emitInlineAsm(StringRef Str, const MCSubtargetInfo &STI,
102102
std::unique_ptr<MCAsmParser> Parser(
103103
createMCAsmParser(SrcMgr, OutContext, *OutStreamer, *MAI, BufNum));
104104

105+
// Do not use assembler-level information for parsing inline assembly.
106+
OutStreamer->setUseAssemblerInfoForParsing(false);
107+
105108
// We create a new MCInstrInfo here since we might be at the module level
106109
// and not have a MachineFunction to initialize the TargetInstrInfo from and
107110
// we only need MCInstrInfo for asm parsing. We create one unconditionally

llvm/lib/MC/MCObjectStreamer.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,14 @@ MCObjectStreamer::MCObjectStreamer(MCContext &Context,
4040

4141
MCObjectStreamer::~MCObjectStreamer() = default;
4242

43-
MCAssembler *MCObjectStreamer::getAssemblerPtr() { return Assembler.get(); }
43+
// AssemblerPtr is used for evaluation of expressions and causes
44+
// difference between asm and object outputs. Return nullptr to in
45+
// inline asm mode to limit divergence to assembly inputs.
46+
MCAssembler *MCObjectStreamer::getAssemblerPtr() {
47+
if (getUseAssemblerInfoForParsing())
48+
return Assembler.get();
49+
return nullptr;
50+
}
4451

4552
void MCObjectStreamer::addPendingLabel(MCSymbol* S) {
4653
MCSection *CurSection = getCurrentSectionOnly();

llvm/lib/MC/MCStreamer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void MCTargetStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {}
9393

9494
MCStreamer::MCStreamer(MCContext &Ctx)
9595
: Context(Ctx), CurrentWinFrameInfo(nullptr),
96-
CurrentProcWinFrameInfoStartIndex(0) {
96+
CurrentProcWinFrameInfoStartIndex(0), UseAssemblerInfoForParsing(false) {
9797
SectionStack.push_back(std::pair<MCSectionSubPair, MCSectionSubPair>());
9898
}
9999

llvm/lib/Target/AMDGPU/AMDGPUAsmPrinter.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,12 @@ bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
517517

518518
DumpCodeInstEmitter = nullptr;
519519
if (STM.dumpCode()) {
520-
// For -dumpcode, get the assembler out of the streamer. This only works
521-
// with -filetype=obj.
520+
// For -dumpcode, get the assembler out of the streamer, even if it does
521+
// not really want to let us have it. This only works with -filetype=obj.
522+
bool SaveFlag = OutStreamer->getUseAssemblerInfoForParsing();
523+
OutStreamer->setUseAssemblerInfoForParsing(true);
522524
MCAssembler *Assembler = OutStreamer->getAssemblerPtr();
525+
OutStreamer->setUseAssemblerInfoForParsing(SaveFlag);
523526
if (Assembler)
524527
DumpCodeInstEmitter = Assembler->getEmitterPtr();
525528
}

llvm/lib/Target/SPIRV/SPIRVAsmPrinter.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,12 @@ void SPIRVAsmPrinter::emitEndOfAsmFile(Module &M) {
114114
// Bound is an approximation that accounts for the maximum used register
115115
// number and number of generated OpLabels
116116
unsigned Bound = 2 * (ST->getBound() + 1) + NLabels;
117+
bool FlagToRestore = OutStreamer->getUseAssemblerInfoForParsing();
118+
OutStreamer->setUseAssemblerInfoForParsing(true);
117119
if (MCAssembler *Asm = OutStreamer->getAssemblerPtr())
118120
Asm->setBuildVersion(static_cast<MachO::PlatformType>(0), Major, Minor,
119121
Bound, VersionTuple(Major, Minor, 0, Bound));
122+
OutStreamer->setUseAssemblerInfoForParsing(FlagToRestore);
120123
}
121124

122125
void SPIRVAsmPrinter::emitFunctionHeader() {

llvm/test/MC/AsmParser/assembler-expressions-inlineasm.ll

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
; RUN: not llc -mtriple=x86_64 %s -o /dev/null 2>&1 | FileCheck %s
2-
; RUN: llc -mtriple=x86_64 -no-integrated-as < %s | FileCheck %s --check-prefix=GAS
3-
; RUN: llc -mtriple=x86_64 -filetype=obj %s -o - | llvm-objdump -d - | FileCheck %s --check-prefix=DISASM
1+
; RUN: not llc -mtriple x86_64-unknown-linux-gnu -o %t.s -filetype=asm %s 2>&1 | FileCheck %s
2+
; RUN: not llc -mtriple x86_64-unknown-linux-gnu -o %t.o -filetype=obj %s 2>&1 | FileCheck %s
43

5-
; GAS: nop; .if . - foo==1; nop;.endif
4+
; Assembler-aware expression evaluation should be disabled in inline
5+
; assembly to prevent differences in behavior between object and
6+
; assembly output.
67

7-
; CHECK: <inline asm>:1:17: error: expected absolute expression
88

9-
; DISASM: <main>:
10-
; DISASM-NEXT: nop
11-
; DISASM-NEXT: nop
12-
; DISASM-NEXT: xorl %eax, %eax
13-
; DISASM-NEXT: retq
9+
; CHECK: <inline asm>:1:17: error: expected absolute expression
1410

1511
define i32 @main() local_unnamed_addr {
1612
tail call void asm sideeffect "foo: nop; .if . - foo==1; nop;.endif", "~{dirflag},~{fpsr},~{flags}"()

llvm/tools/llvm-mc/llvm-mc.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,9 @@ int main(int argc, char **argv) {
569569
Str->initSections(true, *STI);
570570
}
571571

572+
// Use Assembler information for parsing.
573+
Str->setUseAssemblerInfoForParsing(true);
574+
572575
int Res = 1;
573576
bool disassemble = false;
574577
switch (Action) {

llvm/tools/llvm-ml/llvm-ml.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ int llvm_ml_main(int Argc, char **Argv, const llvm::ToolContext &) {
428428
Str->emitAssignment(Feat00Sym, MCConstantExpr::create(Feat00Flags, Ctx));
429429
}
430430

431+
// Use Assembler information for parsing.
432+
Str->setUseAssemblerInfoForParsing(true);
433+
431434
int Res = 1;
432435
if (InputArgs.hasArg(OPT_as_lex)) {
433436
// -as-lex; Lex only, and output a stream of tokens

0 commit comments

Comments
 (0)