LLVMContext::setDiscardValueNames(true) when not compiling to textual IR.

Resolves #1749
This commit is contained in:
Johan Engelen 2016-09-13 14:52:45 +02:00
parent b17e1485ed
commit b70042a85e
4 changed files with 67 additions and 0 deletions

View file

@ -84,6 +84,13 @@ CodeGenerator::CodeGenerator(llvm::LLVMContext &context, bool singleObj)
"configured properly");
fatal();
}
#if LDC_LLVM_VER >= 309
// Set the context to discard value names when not generating textual IR.
if (!global.params.output_ll) {
context_.setDiscardValueNames(true);
}
#endif
}
CodeGenerator::~CodeGenerator() {

View file

@ -16,6 +16,24 @@
namespace {
/// Sets LLVMContext::setDiscardValueNames(false) upon construction and restores
/// the previous value upon destruction.
struct TempDisableDiscardValueNames {
#if LDC_LLVM_VER >= 309
llvm::LLVMContext &ctx;
bool previousValue;
TempDisableDiscardValueNames(llvm::LLVMContext &context)
: ctx(context), previousValue(context.shouldDiscardValueNames()) {
ctx.setDiscardValueNames(false);
}
~TempDisableDiscardValueNames() { ctx.setDiscardValueNames(previousValue); }
#else
TempDisableDiscardValueNames(llvm::LLVMContext &context) {}
#endif
};
/// Adds the idol's function attributes to the wannabe
void copyFnAttributes(llvm::Function *wannabe, llvm::Function *idol) {
auto attrSet = idol->getAttributes();
@ -29,6 +47,10 @@ DValue *DtoInlineIRExpr(Loc &loc, FuncDeclaration *fdecl,
IF_LOG Logger::println("DtoInlineIRExpr @ %s", loc.toChars());
LOG_SCOPE;
// LLVM can't read textual IR with a Context that discards named Values, so
// temporarily disable value name discarding.
TempDisableDiscardValueNames tempDisable(gIR->context());
// Generate a random new function name. Because the inlineIR function is
// always inlined, this name does not escape the current compiled module; not
// even at -O0.

View file

@ -0,0 +1,24 @@
// Test value name discarding when creating non-textual IR.
// REQUIRES: atleast_llvm309
// RUN: %ldc %S/inputs/input_discard_valuename.d -c -output-ll -of=%t.bar.ll && FileCheck %S/inputs/input_discard_valuename.d < %t.bar.ll
// Output a bitcode file (i.e. with discarded names) and input it into a second LDC command that outputs textual IR.
// RUN: %ldc %S/inputs/input_discard_valuename.d -g -c -output-bc -of=%t.bar.bc \
// RUN: && %ldc %s %t.bar.bc -g -c -output-ll -of=%t.ll && FileCheck %s < %t.ll
// IR imported from the bitcode file should not have local value names:
// CHECK-LABEL: define{{.*}} @foo
// CHECK: %localfoovar
// CHECK-LABEL: define{{.*}} @bar
// CHECK-NOT: %localbarvar
// But the imported IR should still have debug names:
// CHECK: DILocalVariable{{.*}}"localfoovar"
// CHECK: DILocalVariable{{.*}}"localbarvar"
extern(C) void foo()
{
int localfoovar;
}

View file

@ -0,0 +1,14 @@
// CHECK-LABEL: define{{.*}} @bar(
extern(C) void bar()
{
// CHECK: localbarvar
int localbarvar;
}
// Make sure we can use inline IR in non-textual IR compiles:
pragma(LDC_inline_ir) R __ir(string s, R, P...)(P);
double inlineIR(double a)
{
auto s = __ir!(`ret double %0`, double)(a);
return s;
}