Run semantic3 on imported modules, and emit new symbols with

`available_externally` linkage. This allows the inliner to inline functions from
other modules while telling the code generator to ignore those functions (treat
them as declarations)
Still generates a few extra `TypeInfo`s and strings...
Disabled when generating debug info because I don't really understand it, and it
doesn't like this.
This commit is contained in:
Frits van Bommel
2009-06-07 16:00:13 +02:00
parent b972dcb92d
commit 96fdcd6347
15 changed files with 201 additions and 9 deletions
+2
View File
@@ -491,7 +491,9 @@ LLValue* DtoIndexClass(LLValue* src, ClassDeclaration* cd, VarDeclaration* vd)
{
Logger::cout() << "src2: " << *src << '\n';
Logger::cout() << "index: " << field->index << '\n';
#if 0
Logger::cout() << "srctype: " << *src->getType() << '\n';
#endif
}
#endif
LLValue* val = DtoGEPi(src, 0, field->index);
-1
View File
@@ -9,6 +9,5 @@ enum
LLVMva_copy,
LLVMva_end,
LLVMva_arg,
LLVMldc,
LLVMinline_asm
};
+42
View File
@@ -1328,6 +1328,48 @@ bool mustDefineSymbol(Dsymbol* s)
if (fd->isArrayOp)
return true;
if (global.params.useAvailableExternally && fd->availableExternally) {
// Emit extra functions if we're inlining.
// These will get available_externally linkage,
// so they shouldn't end up in object code.
assert(fd->type->ty == Tfunction);
TypeFunction* tf = (TypeFunction*) fd->type;
// * If we define extra static constructors, static destructors
// and unittests they'll get registered to run, and we won't
// be calling them directly anyway.
// * If it's a large function, don't emit it unnecessarily.
// Use DMD's canInline() to determine whether it's large.
// inlineCost() members have been changed to pay less attention
// to DMDs limitations, but still have some issues. The most glaring
// offenders are any kind of control flow statements other than
// 'if' and 'return'.
if ( !fd->isStaticCtorDeclaration()
&& !fd->isStaticDtorDeclaration()
&& !fd->isUnitTestDeclaration()
&& fd->canInline(true))
{
return true;
}
// This was only semantic'ed for inlining checks.
// We won't be inlining this, so we only need to emit a declaration.
return false;
}
}
// Inlining checks may create some variable and class declarations
// we don't need to emit.
if (global.params.useAvailableExternally)
{
if (VarDeclaration* vd = s->isVarDeclaration())
if (vd->availableExternally)
return false;
if (ClassDeclaration* cd = s->isClassDeclaration())
if (cd->availableExternally)
return false;
}
TemplateInstance* tinst = DtoIsTemplateInstance(s);
+14 -1
View File
@@ -35,6 +35,7 @@
#include "gen/logger.h"
#include "gen/linker.h"
#include "gen/irstate.h"
#include "gen/optimizer.h"
#include "gen/toobj.h"
#include "gen/metadata.h"
#include "gen/passes/Passes.h"
@@ -811,6 +812,17 @@ int main(int argc, char** argv)
* not be found at link time.
*/
if (!global.params.useArrayBounds && !global.params.useAssert)
#elif LLVM_REV >= 68940
// This doesn't play nice with debug info at the moment
if (!global.params.symdebug && willInline())
{
global.params.useAvailableExternally = true;
Logger::println("Running some extra semantic3's for inlining purposes");
#else
// IN_LLVM, but available_externally not available yet.
if (false)
{
#endif
{
// Do pass 3 semantic analysis on all imported modules,
// since otherwise functions in them cannot be inlined
@@ -825,6 +837,7 @@ int main(int argc, char** argv)
fatal();
}
#if !IN_LLVM
for (int i = 0; i < modules.dim; i++)
{
m = (Module *)modules.data[i];
@@ -832,10 +845,10 @@ int main(int argc, char** argv)
printf("inline scan %s\n", m->toChars());
m->inlineScan();
}
#endif
}
if (global.errors)
fatal();
#endif
// write module dependencies to file if requested
if (global.params.moduleDepsFile != NULL)
+18 -1
View File
@@ -12,6 +12,7 @@
#include "llvm/Support/PassNameParser.h"
#include "root.h" // error()
#include <cstring> // strcmp();
using namespace llvm;
@@ -78,6 +79,19 @@ bool doInline() {
|| (enableInlining == cl::BOU_UNSET && optimizeLevel >= 3);
}
// Determine whether the inliner will be run.
bool willInline() {
if (doInline())
return true;
// It may also have been specified explicitly on the command line as an explicit pass
typedef cl::list<const PassInfo*, bool, PassNameParser> PL;
for (PL::iterator I = passList.begin(), E = passList.end(); I != E; ++I) {
if (!std::strcmp((*I)->getPassArgument(), "inline"))
return true;
}
return false;
}
// Some extra accessors for the linker: (llvm-ld version only, currently unused?)
int optLevel() {
return optimizeLevel;
@@ -108,7 +122,6 @@ static void addPassesForOptLevel(PassManager& pm) {
else
addPass(pm, createScalarReplAggregatesPass());
addPass(pm, createGlobalOptimizerPass());
addPass(pm, createGlobalDCEPass());
}
// -O2
@@ -208,6 +221,10 @@ static void addPassesForOptLevel(PassManager& pm) {
addPass(pm, createConstantMergePass());
}
if (optimizeLevel >= 1) {
addPass(pm, createGlobalDCEPass());
}
// level -O4 and -O5 are linktime optimizations
}
+3
View File
@@ -5,7 +5,10 @@ namespace llvm { class Module; }
bool ldc_optimize_module(llvm::Module* m);
// Determines whether the inliner will run in the -O<N> list of passes
bool doInline();
// Determines whether the inliner will be run at all.
bool willInline();
int optLevel();
+47 -6
View File
@@ -241,6 +241,13 @@ LLGlobalValue::LinkageTypes DtoLinkage(Dsymbol* sym)
// global variable
if (VarDeclaration* vd = sym->isVarDeclaration())
{
if (mustDefineSymbol(vd))
Logger::println("Variable %savailable externally: %s", (vd->availableExternally ? "" : "not "), vd->toChars());
#if LLVM_REV >= 68940
// generated by inlining semantics run
if (vd->availableExternally && mustDefineSymbol(sym))
return llvm::GlobalValue::AvailableExternallyLinkage;
#endif
// template
if (needsTemplateLinkage(sym))
return TEMPLATE_LINKAGE_TYPE;
@@ -248,15 +255,22 @@ LLGlobalValue::LinkageTypes DtoLinkage(Dsymbol* sym)
// function
else if (FuncDeclaration* fdecl = sym->isFuncDeclaration())
{
if (mustDefineSymbol(fdecl))
Logger::println("Function %savailable externally: %s", (fdecl->availableExternally ? "" : "not "), fdecl->toChars());
assert(fdecl->type->ty == Tfunction);
TypeFunction* ft = (TypeFunction*)fdecl->type;
// array operations are always template linkage
if (fdecl->isArrayOp)
return TEMPLATE_LINKAGE_TYPE;
// intrinsics are always external
if (fdecl->llvmInternal == LLVMintrinsic)
return llvm::GlobalValue::ExternalLinkage;
#if LLVM_REV >= 68940
// generated by inlining semantics run
if (fdecl->availableExternally && mustDefineSymbol(sym))
return llvm::GlobalValue::AvailableExternallyLinkage;
#endif
// array operations are always template linkage
if (fdecl->isArrayOp)
return TEMPLATE_LINKAGE_TYPE;
// template instances should have weak linkage
// but only if there's a body, and it's not naked
// otherwise we make it external
@@ -269,6 +283,13 @@ LLGlobalValue::LinkageTypes DtoLinkage(Dsymbol* sym)
// class
else if (ClassDeclaration* cd = sym->isClassDeclaration())
{
if (mustDefineSymbol(cd))
Logger::println("Class %savailable externally: %s", (cd->availableExternally ? "" : "not "), vd->toChars());
#if LLVM_REV >= 68940
// generated by inlining semantics run
if (cd->availableExternally && mustDefineSymbol(sym))
return llvm::GlobalValue::AvailableExternallyLinkage;
#endif
// template
if (needsTemplateLinkage(cd))
return TEMPLATE_LINKAGE_TYPE;
@@ -278,8 +299,8 @@ LLGlobalValue::LinkageTypes DtoLinkage(Dsymbol* sym)
assert(0 && "not global/function");
}
// The following breaks for nested naked functions, so check for that.
bool skipNestedCheck = false;
// The following breaks for nested naked functions and other declarations, so check for that.
bool skipNestedCheck = !mustDefineSymbol(sym);
if (FuncDeclaration* fd = sym->isFuncDeclaration())
skipNestedCheck = (fd->naked != 0);
@@ -306,16 +327,36 @@ LLGlobalValue::LinkageTypes DtoLinkage(Dsymbol* sym)
return llvm::GlobalValue::ExternalLinkage;
}
static bool isAvailableExternally(Dsymbol* sym)
{
if (VarDeclaration* vd = sym->isVarDeclaration())
return vd->availableExternally;
if (FuncDeclaration* fd = sym->isFuncDeclaration())
return fd->availableExternally;
if (AggregateDeclaration* ad = sym->isAggregateDeclaration())
return ad->availableExternally;
return false;
}
llvm::GlobalValue::LinkageTypes DtoInternalLinkage(Dsymbol* sym)
{
if (needsTemplateLinkage(sym))
if (needsTemplateLinkage(sym)) {
#if LLVM_REV >= 68940
if (isAvailableExternally(sym) && mustDefineSymbol(sym))
return llvm::GlobalValue::AvailableExternallyLinkage;
#endif
return TEMPLATE_LINKAGE_TYPE;
}
else
return llvm::GlobalValue::InternalLinkage;
}
llvm::GlobalValue::LinkageTypes DtoExternalLinkage(Dsymbol* sym)
{
#if LLVM_REV >= 68940
if (isAvailableExternally(sym) && mustDefineSymbol(sym))
return llvm::GlobalValue::AvailableExternallyLinkage;
#endif
if (needsTemplateLinkage(sym))
return TEMPLATE_LINKAGE_TYPE;
else
+1
View File
@@ -42,6 +42,7 @@
#include "gen/linkage.h"
#include "gen/metadata.h"
#include "gen/rttibuilder.h"
#include "gen/llvm-version.h"
#include "ir/irvar.h"
#include "ir/irtype.h"