jit: better module stripping (#2743)

This commit is contained in:
Ivan Butygin 2018-06-10 16:54:42 +03:00 committed by GitHub
parent 86265da6b0
commit 65337ed170
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 0 deletions

View file

@ -169,6 +169,22 @@ void iterateFuncInstructions(llvm::Function &func, F &&handler) {
} // for (auto &&bb : fun) } // for (auto &&bb : fun)
} }
template<typename I>
void stripDeclarations(llvm::iterator_range<I> range) {
for (auto it = range.begin(); it != range.end();) {
auto elem = &(*it);
++it;
if (elem->isDeclaration() && elem->use_empty()) {
elem->eraseFromParent();
}
}
}
void stripModule(llvm::Module &module) {
stripDeclarations(module.functions());
stripDeclarations(module.globals());
}
void fixRtModule(llvm::Module &newModule, void fixRtModule(llvm::Module &newModule,
const decltype(IRState::dynamicCompiledFunctions) &funcs) { const decltype(IRState::dynamicCompiledFunctions) &funcs) {
std::unordered_map<std::string, std::string> thunkVar2func; std::unordered_map<std::string, std::string> thunkVar2func;
@ -224,6 +240,8 @@ void fixRtModule(llvm::Module &newModule,
} }
} }
} }
stripModule(newModule);
} }
void removeFunctionsTargets(IRState *irs, llvm::Module &module) { void removeFunctionsTargets(IRState *irs, llvm::Module &module) {

View file

@ -1,6 +1,7 @@
// RUN: %ldc -enable-dynamic-compile -run %s // RUN: %ldc -enable-dynamic-compile -run %s
import std.stdio;
import std.array; import std.array;
import std.string; import std.string;
import ldc.attributes; import ldc.attributes;
@ -8,6 +9,7 @@ import ldc.dynamic_compile;
__gshared int value = 32; __gshared int value = 32;
@dynamicCompile int foo() @dynamicCompile int foo()
{ {
return 42; return 42;
@ -27,6 +29,11 @@ void fun(int function())
fun(&foo); fun(&foo);
} }
@dynamicCompile int bzz()
{
return value;
}
class Foo() class Foo()
{ {
@dynamicCompile int foo() @dynamicCompile int foo()
@ -35,6 +42,27 @@ class Foo()
} }
} }
struct UnusedStruct1
{
int i;
int j;
};
struct UnusedStruct2
{
UnusedStruct1 f;
int k;
}
void unusedfunc1(UnusedStruct2)
{
}
void unusedfunc2(int function())
{
}
__gshared int unusedvalue = 32;
void main(string[] args) void main(string[] args)
{ {
@ -44,10 +72,20 @@ void main(string[] args)
{ {
if (DumpStage.OriginalModule == stage) if (DumpStage.OriginalModule == stage)
{ {
write(str);
dump.put(str); dump.put(str);
} }
}; };
writeln("===========================================");
compileDynamicCode(settings); compileDynamicCode(settings);
writeln("===========================================");
assert(0 == count(dump.data, "thunk")); assert(0 == count(dump.data, "thunk"));
assert(0 == count(dump.data, "unusedvalue"));
assert(0 == count(dump.data, "unusedfunc1"));
assert(0 == count(dump.data, "unusedfunc2"));
// TODO: these symbols is pulled by llvm.ldc.typeinfo
//assert(0 == count(dump.data, "UnusedStruct1"));
//assert(0 == count(dump.data, "UnusedStruct2"));
} }