Skip to content

Commit

Permalink
Linker: Fix linking of byref types
Browse files Browse the repository at this point in the history
This wasn't properly remapping the type like with the other
attributes, so this would end up hitting a verifier error after
linking different modules using byref.
  • Loading branch information
arsenm committed Nov 17, 2020
1 parent e67d885 commit c5ce603
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion llvm/lib/Linker/IRMover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ GlobalVariable *IRLinker::copyGlobalVariableProto(const GlobalVariable *SGVar) {
AttributeList IRLinker::mapAttributeTypes(LLVMContext &C, AttributeList Attrs) {
for (unsigned i = 0; i < Attrs.getNumAttrSets(); ++i) {
for (Attribute::AttrKind TypedAttr :
{Attribute::ByVal, Attribute::StructRet}) {
{Attribute::ByVal, Attribute::StructRet, Attribute::ByRef}) {
if (Attrs.hasAttribute(i, TypedAttr)) {
if (Type *Ty = Attrs.getAttribute(i, TypedAttr).getValueAsType()) {
Attrs = Attrs.replaceAttributeType(C, i, TypedAttr, TypeMap.get(Ty));
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Utils/ValueMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ void Mapper::remapInstruction(Instruction *I) {
AttributeList Attrs = CB->getAttributes();
for (unsigned i = 0; i < Attrs.getNumAttrSets(); ++i) {
for (Attribute::AttrKind TypedAttr :
{Attribute::ByVal, Attribute::StructRet}) {
{Attribute::ByVal, Attribute::StructRet, Attribute::ByRef}) {
if (Type *Ty = Attrs.getAttribute(i, TypedAttr).getValueAsType()) {
Attrs = Attrs.replaceAttributeType(C, i, TypedAttr,
TypeMapper->remapType(Ty));
Expand Down
13 changes: 13 additions & 0 deletions llvm/test/Linker/Inputs/byref-type-input.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
%a = type { i64 }
%struct = type { i32, i8 }

define void @g(%a* byref(%a)) {
ret void
}

declare void @baz(%struct* byref(%struct))

define void @foo(%struct* byref(%struct) %a) {
call void @baz(%struct* byref(%struct) %a)
ret void
}
25 changes: 25 additions & 0 deletions llvm/test/Linker/byref-types.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
; RUN: llvm-link %s %p/Inputs/byref-type-input.ll -S | FileCheck %s

%a = type { i64 }
%struct = type { i32, i8 }

; CHECK-LABEL: define void @f(%a* byref(%a) %0)
define void @f(%a* byref(%a)) {
ret void
}

; CHECK-LABEL: define void @bar(
; CHECK: call void @foo(%struct* byref(%struct) %ptr)
define void @bar() {
%ptr = alloca %struct
call void @foo(%struct* byref(%struct) %ptr)
ret void
}

; CHECK-LABEL: define void @g(%a* byref(%a) %0)

; CHECK-LABEL: define void @foo(%struct* byref(%struct) %a)
; CHECK-NEXT: call void @baz(%struct* byref(%struct) %a)
declare void @foo(%struct* byref(%struct) %a)

; CHECK: declare void @baz(%struct* byref(%struct))

0 comments on commit c5ce603

Please sign in to comment.