[PATCH] Fix getOrInsertGlobal dropping the address space.

Matt Arsenault Matthew.Arsenault at amd.com
Fri Sep 27 18:32:35 PDT 2013


  Fix address space when a bitcast is necessary

http://llvm-reviews.chandlerc.com/D1781

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D1781?vs=4536&id=4537#toc

Files:
  lib/IR/Module.cpp
  unittests/IR/ValueTest.cpp

Index: lib/IR/Module.cpp
===================================================================
--- lib/IR/Module.cpp
+++ lib/IR/Module.cpp
@@ -260,8 +260,10 @@
 
   // If the variable exists but has the wrong type, return a bitcast to the
   // right type.
-  if (GV->getType() != PointerType::getUnqual(Ty))
-    return ConstantExpr::getBitCast(GV, PointerType::getUnqual(Ty));
+  Type *GVTy = GV->getType();
+  PointerType *PTy = PointerType::get(Ty, GVTy->getPointerAddressSpace());
+  if (GV->getType() != PTy)
+    return ConstantExpr::getBitCast(GV, PTy);
 
   // Otherwise, we just found the existing function or a prototype.
   return GV;
Index: unittests/IR/ValueTest.cpp
===================================================================
--- unittests/IR/ValueTest.cpp
+++ unittests/IR/ValueTest.cpp
@@ -43,4 +43,46 @@
   EXPECT_TRUE(F->arg_begin()->isUsedInBasicBlock(F->begin()));
 }
 
+TEST(GlobalTest, CreateAddressSpace) {
+  LLVMContext &Ctx = getGlobalContext();
+  OwningPtr<Module> M(new Module("TestModule", Ctx));
+  Type *Int8Ty = Type::getInt32Ty(Ctx);
+  Type *Int32Ty = Type::getInt32Ty(Ctx);
+
+  GlobalVariable *Dummy0
+    = new GlobalVariable(*M,
+                         Int32Ty,
+                         true,
+                         GlobalValue::ExternalLinkage,
+                         Constant::getAllOnesValue(Int32Ty),
+                         "dummy",
+                         0,
+                         GlobalVariable::NotThreadLocal,
+                         1);
+
+  // Make sure the address space isn't dropped when returning this.
+  Constant *Dummy1 = M->getOrInsertGlobal("dummy", Int32Ty);
+  EXPECT_EQ(Dummy0, Dummy1);
+  EXPECT_EQ(1u, Dummy1->getType()->getPointerAddressSpace());
+
+
+  // This one requires a bitcast, but the address space must also stay the same.
+
+  GlobalVariable *DummyCast0
+    = new GlobalVariable(*M,
+                         Int32Ty,
+                         true,
+                         GlobalValue::ExternalLinkage,
+                         Constant::getAllOnesValue(Int32Ty),
+                         "dummy_cast",
+                         0,
+                         GlobalVariable::NotThreadLocal,
+                         1);
+
+  // Make sure the address space isn't dropped when returning this.
+  Constant *DummyCast1 = M->getOrInsertGlobal("dummy_cast", Int8Ty);
+  EXPECT_EQ(1u, DummyCast1->getType()->getPointerAddressSpace());
+
+
+}
 } // end anonymous namespace
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1781.2.patch
Type: text/x-patch
Size: 2473 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130927/bf074482/attachment.bin>


More information about the llvm-commits mailing list