Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3926 - handle GEPs in DLLImport relocation pass #3927

Merged
merged 1 commit into from
Mar 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions gen/passes/DLLImportRelocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,16 @@ struct Impl {
if (!initializer)
return false;

// set i to the initializer, skipping over an optional cast
auto i = initializer;
if (auto ce = dyn_cast<ConstantExpr>(i)) {
if (ce->isCast())
i = ce->getOperand(0);
// set i to the initializer, descending into GEPs and skipping over casts
auto i = skipOverCast(initializer);
if (auto gep = isGEP(i)) {
i = skipOverCast(gep->getOperand(0));
}

// check if i is a reference to a dllimport global
if (auto globalRef = dyn_cast<GlobalVariable>(i)) {
if (globalRef->hasDLLImportStorageClass()) {
onDLLImportReference(globalRef);
onDLLImportReference(globalRef, initializer);
return true;
}
return false;
Expand All @@ -105,12 +104,13 @@ struct Impl {
}

private:
void onDLLImportReference(GlobalVariable *importedVar) {
void onDLLImportReference(GlobalVariable *importedVar,
Constant *originalInitializer) {
++NumRelocations;

// initialize at runtime:
currentGlobal->setConstant(false);
appendToCRTConstructor(importedVar);
appendToCRTConstructor(importedVar, originalInitializer);

const auto pathLength = currentGepPath.size();
if (pathLength == 0) {
Expand Down Expand Up @@ -176,7 +176,8 @@ struct Impl {
llvm::appendToGlobalCtors(m, ctor, 0);
}

void appendToCRTConstructor(GlobalVariable *importedVar) {
void appendToCRTConstructor(GlobalVariable *importedVar,
Constant *originalInitializer) {
if (!ctor)
createCRTConstructor();

Expand All @@ -197,10 +198,14 @@ struct Impl {
}
}

Value *value = importedVar;
Constant *value = importedVar;
auto t = address->getType()->getPointerElementType();
if (value->getType() != t)
value = b.CreatePointerCast(value, t);
if (auto gep = isGEP(skipOverCast(originalInitializer))) {
Constant *newOperand =
createConstPointerCast(importedVar, gep->getOperand(0)->getType());
value = gep->getWithOperandReplaced(0, newOperand);
}
value = createConstPointerCast(value, t);

// Only modify the field if the current value is still null from the static
// initializer. This is important for multiple definitions of a (templated)
Expand All @@ -224,6 +229,28 @@ struct Impl {
b.CreateStore(value, address);
b.CreateBr(endbb);
}

static Constant *skipOverCast(Constant *value) {
if (auto ce = dyn_cast<ConstantExpr>(value)) {
if (ce->isCast())
return ce->getOperand(0);
}
return value;
}

static ConstantExpr *isGEP(Constant *value) {
if (auto ce = dyn_cast<ConstantExpr>(value)) {
if (ce->getOpcode() == Instruction::GetElementPtr)
return ce;
}
return nullptr;
}

static Constant *createConstPointerCast(Constant *value, Type *type) {
return value->getType() == type
? value
: llvm::ConstantExpr::getPointerCast(value, type);
}
};
}

Expand Down
32 changes: 32 additions & 0 deletions tests/codegen/dllimport_gh3926.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// REQUIRES: Windows

// RUN: %ldc -output-ll -dllimport=all -of=%t_all.ll %s && FileCheck %s < %t_all.ll
// RUN: %ldc -output-ll -dllimport=defaultLibsOnly -of=%t_dlo.ll %s && FileCheck %s < %t_dlo.ll

import std.variant : Variant; // pre-instantiated template

void foo()
{
// define TypeInfo_Struct referencing dllimported Variant vtable and init symbol
auto t = typeid(Variant);
}

// no direct init symbol refs:
// CHECK-NOT: @_D3std7variant__T8VariantN{{.*}}6__initZ

// dllimported init symbol:
// CHECK: @_D3std7variant__T8VariantN{{.*}}6__initZ = external dllimport

// no direct init symbol refs:
// CHECK-NOT: @_D3std7variant__T8VariantN{{.*}}6__initZ

// check generated CRT constructor:
// CHECK: define private void @ldc.dllimport_relocation()

// should set the vptr:
// CHECK: if:
// CHECK-NEXT: store [{{[0-9]+}} x i8*]* @_D15TypeInfo_Struct6__vtblZ,

// should set m_init.ptr:
// CHECK: if1:
// CHECK-NEXT: store i8* getelementptr inbounds ({{.*}} @_D3std7variant__T8VariantN{{.*}}6__initZ, i32 0, i32 0, i32 0),