Skip to content

Commit

Permalink
[EraVM] Ignore constant global variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelKopyl committed Jun 27, 2024
1 parent c207b80 commit 8dbab17
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
35 changes: 15 additions & 20 deletions llvm/lib/Target/EraVM/EraVMAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<std::pair<unsigned, const char *>> 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:
Expand Down Expand Up @@ -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<const GlobalVariable *> 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<MCInst> 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(
Expand Down
7 changes: 3 additions & 4 deletions llvm/test/CodeGen/EraVM/global_initializers.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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

Expand Down

0 comments on commit 8dbab17

Please sign in to comment.