[PATCH] Fix getOrInsertGlobal dropping the address space.

Eli Friedman eli.friedman at gmail.com
Mon Sep 30 13:49:42 PDT 2013


LGTM.

-Eli


On Fri, Sep 27, 2013 at 6:32 PM, Matt Arsenault
<Matthew.Arsenault at amd.com>wrote:

>   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
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130930/88631af3/attachment.html>


More information about the llvm-commits mailing list