diff --git a/llvm/lib/Target/EraVM/EraVMAsmPrinter.cpp b/llvm/lib/Target/EraVM/EraVMAsmPrinter.cpp index 97f1b1c93589..29e725b3c349 100644 --- a/llvm/lib/Target/EraVM/EraVMAsmPrinter.cpp +++ b/llvm/lib/Target/EraVM/EraVMAsmPrinter.cpp @@ -317,10 +317,6 @@ void EraVMAsmPrinter::emitGlobalConstant(const DataLayout &DL, } void EraVMAsmPrinter::emitDefaultLandingPads() { - Twine DefaultUnwind("DEFAULT_UNWIND"); - Twine DefaultFarReturn("DEFAULT_FAR_RETURN"); - Twine DefaultFarRevert("DEFAULT_FAR_REVERT"); - auto EmitLandingPadInst = [this](unsigned Opc, const Twine &LabelName) { MCInst MCI; MCI.setOpcode(Opc); @@ -337,19 +333,18 @@ void EraVMAsmPrinter::emitDefaultLandingPads() { OutStreamer->emitInstruction(MCI, *STI); }; - OutStreamer->switchSection(OutContext.getObjectFileInfo()->getTextSection()); - - // Landing pad for near calls. - if (!OutContext.lookupSymbol(DefaultUnwind)) - EmitLandingPadInst(EraVM::PANICl, DefaultUnwind); + SmallVector> LandingPags = { + {EraVM::PANICl, "DEFAULT_UNWIND"}, + {EraVM::RETrl, "DEFAULT_FAR_RETURN"}, + {EraVM::REVERTrl, "DEFAULT_FAR_REVERT"}}; - // Landing pad for returns. - if (!OutContext.lookupSymbol(DefaultFarReturn)) - EmitLandingPadInst(EraVM::RETrl, DefaultFarReturn); + OutStreamer->switchSection(OutContext.getObjectFileInfo()->getTextSection()); - // Landing pad for reverts. - if (!OutContext.lookupSymbol(DefaultFarRevert)) - EmitLandingPadInst(EraVM::REVERTrl, DefaultFarRevert); + for (const auto &LP : LandingPags) { + if (MCSymbol *S = OutContext.lookupSymbol(LP.second); + !S || S->isUndefined()) + EmitLandingPadInst(LP.first, LP.second); + } } // Creates instruction: @@ -470,21 +465,21 @@ size_t EraVMAsmPrinter::createInitializeInsts(const GlobalVariable *G, void EraVMAsmPrinter::emitStartOfAsmFile(Module &M) { // Create global variables' initializers in the .rodata section. // .data: .glob_var -> .rodata: .glob_var_initializer + SmallVector GlobalsToInitialize; for (const auto &G : M.globals()) { - if (G.hasInitializer()) { + if (!G.isConstant() && G.hasInitializer()) { MCSymbol *InitSym = OutContext.getOrCreateSymbol(G.getName() + Twine("_initializer")); insertSymbolToConstantMap(G.getInitializer(), InitSym); GlobInitializerMap.insert({G.getInitializer(), InitSym}); + GlobalsToInitialize.emplace_back(&G); } } size_t NumStackElmsToReserve = 0; SmallVector InitInsts; - for (const auto &G : M.globals()) { - if (G.hasInitializer()) - NumStackElmsToReserve += createInitializeInsts(&G, InitInsts); - } + for (const GlobalVariable *G : GlobalsToInitialize) + NumStackElmsToReserve += createInitializeInsts(G, InitInsts); if (NumStackElmsToReserve) { OutStreamer->switchSection( diff --git a/llvm/test/CodeGen/EraVM/global_initializers.ll b/llvm/test/CodeGen/EraVM/global_initializers.ll index b6ef272628d5..835568ad4f9f 100644 --- a/llvm/test/CodeGen/EraVM/global_initializers.ll +++ b/llvm/test/CodeGen/EraVM/global_initializers.ll @@ -3,14 +3,11 @@ target datalayout = "E-p:256:256-i256:256:256-S32-a:256:256" target triple = "eravm" -%ST.type = type { i256, [3 x i256], i256* } - @glob = global i256 113 @glob.arr = global [4 x i256] [i256 1, i256 29, i256 37, i256 4] @glob_ptr_as3 = global i256* zeroinitializer @glob.arr.as4 = addrspace(4) global [4 x i256] zeroinitializer -; TODO: Do we need to support structs? -;@glob.struct = global %ST.type { i256 12, [3 x i256] [i256 101, i256 109, i256 137], i256 * zeroinitializer } +@glob.const = constant i256 737 ; CHECK-LABEL: .text ; CHECK-NEXT: nop stack+=[10 + r0] @@ -36,6 +33,8 @@ target triple = "eravm" ; CHECK-LABEL: glob.arr.as4_initializer: ; CHECK-NEXT: .zero 128 +; CHECK-NOT: glob.const_initializer: + ; CHECK-LABEL: DEFAULT_UNWIND: ; CHECK-NEXT: ret.panic.to_label r0, @DEFAULT_UNWIND