[pkg-d-commits] [ldc] 08/211: LLVMContext::setDiscardValueNames(true) when not compiling to textual IR.

Matthias Klumpp mak at moszumanska.debian.org
Sun Apr 23 22:36:04 UTC 2017


This is an automated email from the git hooks/post-receive script.

mak pushed a commit to annotated tag v1.1.0
in repository ldc.

commit b70042a85e71cb8cd516813f3f86c4dcaa1aef16
Author: Johan Engelen <jbc.engelen at gmail.com>
Date:   Tue Sep 13 14:52:45 2016 +0200

    LLVMContext::setDiscardValueNames(true) when not compiling to textual IR.
    
    Resolves #1749
---
 driver/codegenerator.cpp                       |  7 +++++++
 gen/inlineir.cpp                               | 22 ++++++++++++++++++++++
 tests/codegen/discard_value_names_gh1749.d     | 24 ++++++++++++++++++++++++
 tests/codegen/inputs/input_discard_valuename.d | 14 ++++++++++++++
 4 files changed, 67 insertions(+)

diff --git a/driver/codegenerator.cpp b/driver/codegenerator.cpp
index e5d9edf..55602f3 100644
--- a/driver/codegenerator.cpp
+++ b/driver/codegenerator.cpp
@@ -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() {
diff --git a/gen/inlineir.cpp b/gen/inlineir.cpp
index 0ea6945..31928ac 100644
--- a/gen/inlineir.cpp
+++ b/gen/inlineir.cpp
@@ -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.
diff --git a/tests/codegen/discard_value_names_gh1749.d b/tests/codegen/discard_value_names_gh1749.d
new file mode 100644
index 0000000..c510641
--- /dev/null
+++ b/tests/codegen/discard_value_names_gh1749.d
@@ -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;
+}
diff --git a/tests/codegen/inputs/input_discard_valuename.d b/tests/codegen/inputs/input_discard_valuename.d
new file mode 100644
index 0000000..a24a6f4
--- /dev/null
+++ b/tests/codegen/inputs/input_discard_valuename.d
@@ -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;
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-d/ldc.git



More information about the pkg-d-commits mailing list