@@ -32,45 +32,51 @@ std::unique_ptr<LLVMTargetMachine> createTargetMachine() {
3232class BytecodeSizeTest : public testing ::Test {
3333protected:
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
6569TEST_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
7078const 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