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 function pointers/delegates on Harvard architectures #4465

Merged
merged 4 commits into from
Aug 16, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Supports LLVM 11.0 - 15.0.

#### Bug fixes
- Fix function pointers/delegates on Harvard architectures (e.g., AVR). (#4432, #4465)

# LDC 1.33.0 (2023-07-23)

Expand Down
11 changes: 5 additions & 6 deletions gen/declarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -419,17 +419,16 @@ class CodegenVisitor : public Visitor {
std::string arg = ("/DEFAULTLIB:\"" + name + "\"").str();
gIR->addLinkerOption(llvm::StringRef(arg));
} else {
bool isStaticLib = name.endswith(".a");
const bool isStaticLib = name.endswith(".a");
const size_t nameLen = name.size();

size_t const nameLen = name.size();
size_t const n = nameLen + 3;
char *arg = nullptr;

if (isStaticLib == false) {
if (!isStaticLib) { // name => -lname
const size_t n = nameLen + 3;
arg = static_cast<char *>(mem.xmalloc(n));
arg[0] = '-';
arg[1] = 'l';
memcpy(arg + 2, name.data(), name.size());
memcpy(arg + 2, name.data(), nameLen);
arg[n - 1] = 0;
} else {
arg = static_cast<char *>((mem.xmalloc(nameLen + 1)));
Expand Down
6 changes: 3 additions & 3 deletions gen/tocall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ class ImplicitArgumentsBuilder {

////////////////////////////////////////////////////////////////////////////////

static LLValue *DtoCallableValue(llvm::FunctionType * ft,DValue *fn) {
static LLValue *DtoCallableValue(DValue *fn) {
Type *type = fn->type->toBasetype();
if (type->ty == TY::Tfunction) {
return DtoRVal(fn);
Expand All @@ -829,7 +829,7 @@ static LLValue *DtoCallableValue(llvm::FunctionType * ft,DValue *fn) {
LLValue *dg = DtoLVal(fn);
llvm::StructType *st = isaStruct(DtoType(fn->type));
LLValue *funcptr = DtoGEP(st, dg, 0, 1);
return DtoLoad(ft->getPointerTo(), funcptr, ".funcptr");
return DtoLoad(st->getElementType(1), funcptr, ".funcptr");
}
LLValue *dg = DtoRVal(fn);
assert(isaStruct(dg));
Expand Down Expand Up @@ -862,7 +862,7 @@ DValue *DtoCallFunction(const Loc &loc, Type *resulttype, DValue *fnval,
}

// get callee llvm value
LLValue *callable = DtoCallableValue(irFty.funcType, fnval);
LLValue *callable = DtoCallableValue(fnval);
LLFunctionType *callableTy = irFty.funcType;
if (dfnval && dfnval->func->isCsymbol()) {
// See note in DtoDeclareFunction about K&R foward declared (void) functions
Expand Down
7 changes: 6 additions & 1 deletion ir/irtype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,14 @@ IrTypePointer *IrTypePointer::get(Type *dt) {
assert(!ctype);

LLType *elemType;
unsigned addressSpace = 0;
if (dt->ty == TY::Tnull) {
elemType = llvm::Type::getInt8Ty(getGlobalContext());
} else {
elemType = DtoMemType(dt->nextOf());
if (dt->nextOf()->ty == TY::Tfunction) {
addressSpace = gDataLayout->getProgramAddressSpace();
}

// DtoType could have already created the same type, e.g. for
// dt == Node* in struct Node { Node* n; }.
Expand All @@ -130,7 +134,8 @@ IrTypePointer *IrTypePointer::get(Type *dt) {
}
}

auto t = new IrTypePointer(dt, llvm::PointerType::get(elemType, 0));
auto t =
new IrTypePointer(dt, llvm::PointerType::get(elemType, addressSpace));
ctype = t;
return t;
}
Expand Down
3 changes: 2 additions & 1 deletion ir/irtypefunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ IrTypeDelegate *IrTypeDelegate::get(Type *t) {
IrFuncTy irFty(tf);
llvm::Type *ltf =
DtoFunctionType(tf, irFty, nullptr, Type::tvoid->pointerTo());
llvm::Type *types[] = {getVoidPtrType(), getPtrToType(ltf)};
llvm::Type *fptr = ltf->getPointerTo(gDataLayout->getProgramAddressSpace());
llvm::Type *types[] = {getVoidPtrType(), fptr};
LLStructType *lt = LLStructType::get(gIR->context(), types, false);

// Could have already built the type as part of a struct forward reference,
Expand Down
37 changes: 37 additions & 0 deletions tests/codegen/funcptr_harvard_gh4432.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Tests function pointers/delegates on a Harvard architecture,
// with code residing in a separate address space.

// REQUIRES: target_AVR
// RUN: %ldc -mtriple=avr -betterC -output-ll -of=%t.ll %s && FileCheck %s < %t.ll
// RUN: %ldc -mtriple=avr -betterC -c %s

alias FP = void function();
alias DG = void delegate();

// CHECK: @_D22funcptr_harvard_gh44328globalFPPFZv = global {{void \(\) addrspace\(1\)\*|ptr addrspace\(1\)}} @_D22funcptr_harvard_gh44323barFZv, align 2
__gshared FP globalFP = &bar;
// CHECK: @_D22funcptr_harvard_gh443217globalDataPointerPPFZv = global {{void \(\) addrspace\(1\)\*\*|ptr}} @_D22funcptr_harvard_gh44328globalFPPFZv, align 2
__gshared FP* globalDataPointer = &globalFP;

// CHECK: define void @_D22funcptr_harvard_gh44323fooFPFZvDQeZv({{.*}} addrspace(1){{\*?}} %fp_arg, { {{.*}} addrspace(1){{\*?}} } %dg_arg) addrspace(1)
void foo(FP fp, DG dg)
{
// CHECK: call addrspace(1) void %1()
fp();
// CHECK: call addrspace(1) void %.funcptr
dg();
// CHECK-NEXT: ret void
}

void bar()
{
foo(() {}, delegate() {});

FP fp = &bar;
DG dg;
dg.funcptr = &bar;
foo(fp, dg);

dg.funcptr = *globalDataPointer;
foo(globalFP, dg);
}