Skip to content

Commit

Permalink
Strip target dependent info from IR refs (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Feb 24, 2024
1 parent bd2e685 commit 4efb183
Show file tree
Hide file tree
Showing 151 changed files with 35 additions and 331 deletions.
8 changes: 4 additions & 4 deletions src/SourceFile.cpp
Expand Up @@ -298,7 +298,7 @@ void SourceFile::runIRGenerator() {
irGenerator.visit(ast);

// Save the ir string in the compiler output
compilerOutput.irString = IRGenerator::getIRString(llvmModule.get());
compilerOutput.irString = IRGenerator::getIRString(llvmModule.get(), resourceManager.cliOptions.testMode);

// Dump unoptimized IR code
if (resourceManager.cliOptions.dumpSettings.dumpIR)
Expand Down Expand Up @@ -330,7 +330,7 @@ void SourceFile::runDefaultIROptimizer() {
irOptimizer.optimizeDefault();

// Save the optimized ir string in the compiler output
compilerOutput.irOptString = IRGenerator::getIRString(llvmModule.get());
compilerOutput.irOptString = IRGenerator::getIRString(llvmModule.get(), resourceManager.cliOptions.testMode);

// Dump optimized IR code
if (resourceManager.cliOptions.dumpSettings.dumpIR)
Expand Down Expand Up @@ -361,7 +361,7 @@ void SourceFile::runPreLinkIROptimizer() {
irOptimizer.optimizePreLink();

// Save the optimized ir string in the compiler output
compilerOutput.irOptString = IRGenerator::getIRString(llvmModule.get());
compilerOutput.irOptString = IRGenerator::getIRString(llvmModule.get(), resourceManager.cliOptions.testMode);

// Dump optimized IR code
if (resourceManager.cliOptions.dumpSettings.dumpIR)
Expand Down Expand Up @@ -416,7 +416,7 @@ void SourceFile::runPostLinkIROptimizer() {

// Save the optimized ir string in the compiler output
llvm::Module *module = resourceManager.ltoModule.get();
compilerOutput.irOptString = IRGenerator::getIRString(module);
compilerOutput.irOptString = IRGenerator::getIRString(module, resourceManager.cliOptions.testMode);

// Dump optimized IR code
if (resourceManager.cliOptions.dumpSettings.dumpIR)
Expand Down
18 changes: 17 additions & 1 deletion src/irgenerator/IRGenerator.cpp
Expand Up @@ -546,13 +546,29 @@ void IRGenerator::materializeConstant(LLVMExprResult &exprResult) {
exprResult.value = exprResult.constant;
}

std::string IRGenerator::getIRString(llvm::Module *llvmModule) {
std::string IRGenerator::getIRString(llvm::Module *llvmModule, bool withoutTargetData) {
assert(llvmModule != nullptr); // Make sure the module hasn't been moved away

// Backup target triple and data layout
const std::string targetTriple = llvmModule->getTargetTriple();
const std::string targetDataLayout = llvmModule->getDataLayoutStr();
// Remove target triple and data layout
if (withoutTargetData) {
llvmModule->setTargetTriple("");
llvmModule->setDataLayout("");
}

// Get IR string
std::string output;
llvm::raw_string_ostream oss(output);
llvmModule->print(oss, nullptr);

// Restore target triple and data layout
if (withoutTargetData) {
llvmModule->setTargetTriple(targetTriple);
llvmModule->setDataLayout(targetDataLayout);
}

return output;
}

Expand Down
2 changes: 1 addition & 1 deletion src/irgenerator/IRGenerator.h
Expand Up @@ -124,7 +124,7 @@ class IRGenerator : private CompilerPass, public ParallelizableASTVisitor {
llvm::Value *resolveAddress(const ASTNode *node);
llvm::Value *resolveAddress(LLVMExprResult &exprResult);
[[nodiscard]] llvm::Constant *getDefaultValueForSymbolType(const SymbolType &symbolType);
[[nodiscard]] static std::string getIRString(llvm::Module *llvmModule);
[[nodiscard]] static std::string getIRString(llvm::Module *llvmModule, bool withoutTargetInfo);

private:
// Private methods
Expand Down
36 changes: 13 additions & 23 deletions test/TestRunner.cpp
Expand Up @@ -123,9 +123,6 @@ void execTestCase(const TestCase &testCase) {
TestUtil::checkRefMatch(
testCase.testPath / REF_NAME_IR, [&]() { return mainSourceFile->compilerOutput.irString; },
[&](std::string &expectedOutput, std::string &actualOutput) {
// Cut of first few lines to be target independent
TestUtil::eraseIRModuleHeader(expectedOutput);
TestUtil::eraseIRModuleHeader(actualOutput);
if (cliOptions.generateDebugInfo) {
// Remove the lines, containing paths on the local file system
TestUtil::eraseLinesBySubstring(expectedOutput, " = !DIFile(filename:");
Expand All @@ -135,26 +132,19 @@ void execTestCase(const TestCase &testCase) {

// Check optimized IR code
for (uint8_t i = 1; i <= 5; i++) {
TestUtil::checkRefMatch(
testCase.testPath / REF_NAME_OPT_IR[i - 1],
[&]() {
cliOptions.optLevel = static_cast<OptLevel>(i);

if (cliOptions.useLTO) {
mainSourceFile->runPreLinkIROptimizer();
mainSourceFile->runBitcodeLinker();
mainSourceFile->runPostLinkIROptimizer();
} else {
mainSourceFile->runDefaultIROptimizer();
}

return mainSourceFile->compilerOutput.irOptString;
},
[&](std::string &expectedOutput, std::string &actualOutput) {
// Cut of first n lines to be target independent
TestUtil::eraseIRModuleHeader(expectedOutput);
TestUtil::eraseIRModuleHeader(actualOutput);
});
TestUtil::checkRefMatch(testCase.testPath / REF_NAME_OPT_IR[i - 1], [&]() {
cliOptions.optLevel = static_cast<OptLevel>(i);

if (cliOptions.useLTO) {
mainSourceFile->runPreLinkIROptimizer();
mainSourceFile->runBitcodeLinker();
mainSourceFile->runPostLinkIROptimizer();
} else {
mainSourceFile->runDefaultIROptimizer();
}

return mainSourceFile->compilerOutput.irOptString;
});
}

// Link the bitcode if not happened yet
Expand Down
2 changes: 0 additions & 2 deletions test/test-files/benchmark/success-ackermann/ir-code-O3.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [36 x i8] c"Ackermann of base m=%d and n=%d: %d\00", align 1

Expand Down
2 changes: 0 additions & 2 deletions test/test-files/benchmark/success-ackermann/ir-code.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [36 x i8] c"Ackermann of base m=%d and n=%d: %d\00", align 1

Expand Down
2 changes: 0 additions & 2 deletions test/test-files/benchmark/success-faculty/ir-code-O3.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [21 x i8] c"Faculty of %d is: %d\00", align 1

Expand Down
2 changes: 0 additions & 2 deletions test/test-files/benchmark/success-faculty/ir-code.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [21 x i8] c"Faculty of %d is: %d\00", align 1

Expand Down
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

%struct.Thread = type { { ptr, ptr }, i64 }

Expand Down
2 changes: 0 additions & 2 deletions test/test-files/benchmark/success-fibonacci/ir-code-O3.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [11 x i8] c"Result: %d\00", align 1

Expand Down
2 changes: 0 additions & 2 deletions test/test-files/benchmark/success-fibonacci/ir-code.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [11 x i8] c"Result: %d\00", align 1

Expand Down
2 changes: 0 additions & 2 deletions test/test-files/benchmark/success-hello-world/ir-code-O2.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [13 x i8] c"Hello World!\00", align 1

Expand Down
2 changes: 0 additions & 2 deletions test/test-files/benchmark/success-hello-world2/ir-code-O3.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

; Function Attrs: noinline nounwind optnone uwtable
define dso_local i32 @main() local_unnamed_addr #0 {
Expand Down
2 changes: 0 additions & 2 deletions test/test-files/benchmark/success-pidigits/ir-code-O1.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [3 x i8] c"%d\00", align 1

Expand Down
2 changes: 0 additions & 2 deletions test/test-files/benchmark/success-pidigits/ir-code-O2.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [3 x i8] c"%d\00", align 1

Expand Down
2 changes: 0 additions & 2 deletions test/test-files/benchmark/success-pidigits/ir-code-O3.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [3 x i8] c"%d\00", align 1

Expand Down
2 changes: 0 additions & 2 deletions test/test-files/benchmark/success-pidigits/ir-code-Os.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [3 x i8] c"%d\00", align 1

Expand Down
2 changes: 0 additions & 2 deletions test/test-files/benchmark/success-pidigits/ir-code-Oz.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [3 x i8] c"%d\00", align 1

Expand Down
2 changes: 0 additions & 2 deletions test/test-files/benchmark/success-pidigits/ir-code.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [3 x i8] c"%d\00", align 1
@printf.str.1 = private unnamed_addr constant [2 x i8] c".\00", align 1
Expand Down
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [14 x i8] c"Test var: %d\0A\00", align 1

Expand Down
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [11 x i8] c"Param: %d\0A\00", align 1

Expand Down
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [15 x i8] c"Cell [1,3]: %d\00", align 1

Expand Down
2 changes: 0 additions & 2 deletions test/test-files/irgenerator/arrays/success-arrays/ir-code.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [23 x i8] c"Item 0: %d, item 2: %d\00", align 1

Expand Down
2 changes: 0 additions & 2 deletions test/test-files/irgenerator/arrays/success-arrays2/ir-code.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@anon.array.0 = private unnamed_addr constant [10 x i32] [i32 1, i32 2, i32 4, i32 8, i32 16, i32 32, i32 64, i32 128, i32 256, i32 512]
@printf.str.0 = private unnamed_addr constant [17 x i8] c"intArray[3]: %d\0A\00", align 1
Expand Down
2 changes: 0 additions & 2 deletions test/test-files/irgenerator/arrays/success-arrays3/ir-code.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@anon.array.0 = private unnamed_addr constant [2 x i32] [i32 1, i32 2]
@printf.str.0 = private unnamed_addr constant [17 x i8] c"intArray[1]: %d\0A\00", align 1
Expand Down
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@anon.string.0 = private unnamed_addr constant [5 x i8] c"test\00", align 1
@printf.str.0 = private unnamed_addr constant [10 x i8] c"Char: %c\0A\00", align 1
Expand Down
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@anon.string.0 = private unnamed_addr constant [55 x i8] c"Assertion failed: Condition 'true' evaluated to false.\00", align 1
@printf.str.0 = private unnamed_addr constant [26 x i8] c"First assertion was true\0A\00", align 1
Expand Down
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [25 x i8] c"Alignment of double: %d\0A\00", align 1
@printf.str.1 = private unnamed_addr constant [22 x i8] c"Alignment of int: %d\0A\00", align 1
Expand Down
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [25 x i8] c"Alignment of double: %d\0A\00", align 1
@printf.str.1 = private unnamed_addr constant [22 x i8] c"Alignment of int: %d\0A\00", align 1
Expand Down
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [18 x i8] c"Array length: %d\0A\00", align 1

Expand Down
2 changes: 0 additions & 2 deletions test/test-files/irgenerator/builtins/success-len/ir-code.ll
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@anon.array.0 = private unnamed_addr constant [4 x i32] [i32 1, i32 2, i32 3, i32 4]
@printf.str.0 = private unnamed_addr constant [18 x i8] c"Array length: %d\0A\00", align 1
Expand Down
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [13 x i8] c"Hello World!\00", align 1

Expand Down
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [13 x i8] c"Hello World!\00", align 1

Expand Down
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [20 x i8] c"Size of double: %d\0A\00", align 1
@printf.str.1 = private unnamed_addr constant [17 x i8] c"Size of int: %d\0A\00", align 1
Expand Down
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [20 x i8] c"Size of double: %d\0A\00", align 1
@printf.str.1 = private unnamed_addr constant [17 x i8] c"Size of int: %d\0A\00", align 1
Expand Down
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

@printf.str.0 = private unnamed_addr constant [10 x i8] c"Argc: %d\0A\00", align 1
@printf.str.1 = private unnamed_addr constant [16 x i8] c"Argv no. 0: %s\0A\00", align 1
Expand Down
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

%struct.Vector = type { %interface.IIterable, ptr, i64, i64 }
%interface.IIterable = type { ptr }
Expand Down
@@ -1,7 +1,5 @@
; ModuleID = 'source.spice'
source_filename = "source.spice"
target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-w64-windows-gnu"

%struct.TestStruct = type { i64, %struct.String, i32 }
%struct.String = type { ptr, i64, i64 }
Expand Down

0 comments on commit 4efb183

Please sign in to comment.