Skip to content

Commit 6ebf013

Browse files
PavelKopylakiramenai
authored andcommitted
[EVM] Include read-only section size in module size calculation
1 parent 43ce0cc commit 6ebf013

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed

llvm/lib/Target/EVM/EVMCalculateModuleSize.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ uint64_t llvm::EVM::calculateFunctionCodeSize(const MachineFunction &MF) {
123123
return Size;
124124
}
125125

126+
static uint64_t calculateReadOnlyDataSize(const Module &M) {
127+
uint64_t Size = 0;
128+
for (const GlobalVariable &GV : M.globals()) {
129+
if (GV.getAddressSpace() != EVMAS::AS_CODE || !GV.hasInitializer())
130+
continue;
131+
132+
if (const auto *CV = dyn_cast<ConstantDataSequential>(GV.getInitializer()))
133+
Size += CV->getRawDataValues().size();
134+
}
135+
return Size;
136+
}
137+
126138
uint64_t llvm::EVM::calculateModuleCodeSize(Module &M,
127139
const MachineModuleInfo &MMI) {
128140
uint64_t TotalSize = 0;
@@ -133,6 +145,8 @@ uint64_t llvm::EVM::calculateModuleCodeSize(Module &M,
133145
TotalSize += llvm::EVM::calculateFunctionCodeSize(*MF);
134146
}
135147

148+
// Take into account the read-only data that we append to the .text section.
149+
TotalSize += calculateReadOnlyDataSize(M);
136150
// Take into account INVALID instruction at the end of the .text section.
137151
TotalSize++;
138152
return TotalSize;

llvm/unittests/Target/EVM/BytecodeSize.cpp

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,45 +32,51 @@ std::unique_ptr<LLVMTargetMachine> createTargetMachine() {
3232
class BytecodeSizeTest : public testing::Test {
3333
protected:
3434
static const char *MIRString;
35+
static const char *MIRStringReadOnlyData;
3536
LLVMContext Context;
3637
std::unique_ptr<LLVMTargetMachine> TM;
3738
std::unique_ptr<MachineModuleInfo> MMI;
38-
std::unique_ptr<MIRParser> Parser;
39-
std::unique_ptr<Module> M;
4039

4140
static void SetUpTestCase() {
4241
LLVMInitializeEVMTargetInfo();
4342
LLVMInitializeEVMTarget();
4443
LLVMInitializeEVMTargetMC();
4544
}
4645

47-
void SetUp() override {
48-
TM = createTargetMachine();
49-
std::unique_ptr<MemoryBuffer> MBuffer =
50-
MemoryBuffer::getMemBuffer(MIRString);
51-
Parser = createMIRParser(std::move(MBuffer), Context);
46+
std::unique_ptr<Module> parseMIR(StringRef MIR) {
47+
std::unique_ptr<MemoryBuffer> MBuffer = MemoryBuffer::getMemBuffer(MIR);
48+
std::unique_ptr<MIRParser> Parser =
49+
createMIRParser(std::move(MBuffer), Context);
5250
if (!Parser)
5351
report_fatal_error("null MIRParser");
54-
M = Parser->parseIRModule();
52+
std::unique_ptr<Module> M = Parser->parseIRModule();
5553
if (!M)
5654
report_fatal_error("parseIRModule failed");
5755
M->setTargetTriple(TM->getTargetTriple().getTriple());
5856
M->setDataLayout(TM->createDataLayout());
59-
MMI = std::make_unique<MachineModuleInfo>(TM.get());
6057
if (Parser->parseMachineFunctions(*M, *MMI))
6158
report_fatal_error("parseMachineFunctions failed");
59+
60+
return M;
61+
}
62+
63+
void SetUp() override {
64+
TM = createTargetMachine();
65+
MMI = std::make_unique<MachineModuleInfo>(TM.get());
6266
}
6367
};
6468

6569
TEST_F(BytecodeSizeTest, Test) {
66-
uint64_t Size = EVM::calculateModuleCodeSize(*M, *MMI);
67-
ASSERT_TRUE(Size == 131);
70+
std::unique_ptr<Module> M(parseMIR(MIRString));
71+
ASSERT_TRUE(EVM::calculateModuleCodeSize(*M, *MMI) == 131);
72+
73+
std::unique_ptr<Module> M2(parseMIR(MIRStringReadOnlyData));
74+
// 23 = 21 (global variable initializers) + JUMPDEST + INVALID
75+
ASSERT_TRUE(EVM::calculateModuleCodeSize(*M2, *MMI) == 23);
6876
}
6977

7078
const char *BytecodeSizeTest::MIRString = R"MIR(
7179
--- |
72-
; ModuleID = '/Users/pavelkopyl/projects/mlab/era-compiler-tester/tests/llvm/evm/complex/egcd.ll'
73-
source_filename = "/Users/pavelkopyl/projects/mlab/era-compiler-tester/tests/llvm/evm/complex/egcd.ll"
7480
target datalayout = "E-p:256:256-i256:256:256-S256-a:256:256"
7581
target triple = "evm-unknown-unknown"
7682
@@ -358,4 +364,38 @@ body: |
358364
...
359365
)MIR";
360366

367+
const char *BytecodeSizeTest::MIRStringReadOnlyData = R"MIR(
368+
--- |
369+
target datalayout = "E-p:256:256-i256:256:256-S256-a:256:256"
370+
target triple = "evm"
371+
372+
@code_const.1 = private unnamed_addr addrspace(4) constant [7 x i8] c"global1"
373+
@code_const.2 = private unnamed_addr addrspace(4) constant [7 x i8] c"global2"
374+
@code_const.3 = private unnamed_addr addrspace(4) constant [7 x i8] c"global3"
375+
376+
; The following globals are never generated and should not be taken into account.
377+
@heap_const = private unnamed_addr addrspace(1) constant [7 x i8] c"invalid"
378+
@code_global = addrspace(4) global i256 0
379+
@code_global_ext = external addrspace(4) global i256
380+
381+
define void @test() noreturn {
382+
unreachable
383+
}
384+
385+
...
386+
---
387+
name: test
388+
alignment: 1
389+
tracksRegLiveness: true
390+
machineFunctionInfo:
391+
isStackified: true
392+
numberOfParameters: 0
393+
hasPushDeployAddress: false
394+
body: |
395+
bb.0 (%ir-block.0):
396+
liveins: $arguments, $value_stack
397+
398+
...
399+
)MIR";
400+
361401
} // anonymous namespace

0 commit comments

Comments
 (0)