Linker: Fix linking of byref types
authorMatt Arsenault <Matthew.Arsenault@amd.com>
Tue, 17 Nov 2020 15:47:43 +0000 (10:47 -0500)
committerMatt Arsenault <Matthew.Arsenault@amd.com>
Tue, 17 Nov 2020 16:02:04 +0000 (11:02 -0500)
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.

llvm/lib/Linker/IRMover.cpp
llvm/lib/Transforms/Utils/ValueMapper.cpp
llvm/test/Linker/Inputs/byref-type-input.ll [new file with mode: 0644]
llvm/test/Linker/byref-types.ll [new file with mode: 0644]

index 4701850..953f2c3 100644 (file)
@@ -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));
index b2cb8cb..ec57693 100644 (file)
@@ -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));
diff --git a/llvm/test/Linker/Inputs/byref-type-input.ll b/llvm/test/Linker/Inputs/byref-type-input.ll
new file mode 100644 (file)
index 0000000..1ca8a89
--- /dev/null
@@ -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
+}
diff --git a/llvm/test/Linker/byref-types.ll b/llvm/test/Linker/byref-types.ll
new file mode 100644 (file)
index 0000000..b79fb2d
--- /dev/null
@@ -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))