llvm.org GIT mirror llvm / 6fd1b8e
Allow aliases to be unnamed_addr. Alias with unnamed_addr were in a strange state. It is stored in GlobalValue, the language reference talks about "unnamed_addr aliases" but the verifier was rejecting them. It seems natural to allow unnamed_addr in aliases: * It is a property of how it is accessed, not of the data itself. * It is perfectly possible to write code that depends on the address of an alias. This patch then makes unname_addr legal for aliases. One side effect is that the syntax changes for a corner case: In globals, unnamed_addr is now printed before the address space. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@210302 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
20 changed file(s) with 75 addition(s) and 53 deletion(s). Raw diff Collapse all Expand all
697697 might not correctly handle dropping a weak symbol that is aliased.
698698
699699 Alias that are not ``unnamed_addr`` are guaranteed to have the same address as
700 the aliasee.
700 the aliasee expression. ``unnamed_addr`` ones are only guaranteed to point
701 to the same content.
701702
702703 Since aliases are only a second name, some restrictions apply, of which
703704 some can only be checked when producing an object file:
267267 case lltok::kw_constant: // GlobalType
268268 case lltok::kw_global: { // GlobalType
269269 unsigned Linkage, Visibility, DLLStorageClass;
270 bool UnnamedAddr;
270271 GlobalVariable::ThreadLocalMode TLM;
271272 bool HasLinkage;
272273 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
273274 ParseOptionalVisibility(Visibility) ||
274275 ParseOptionalDLLStorageClass(DLLStorageClass) ||
275276 ParseOptionalThreadLocal(TLM) ||
277 parseOptionalUnnamedAddr(UnnamedAddr) ||
276278 ParseGlobal("", SMLoc(), Linkage, HasLinkage, Visibility,
277 DLLStorageClass, TLM))
279 DLLStorageClass, TLM, UnnamedAddr))
278280 return true;
279281 break;
280282 }
466468 bool HasLinkage;
467469 unsigned Linkage, Visibility, DLLStorageClass;
468470 GlobalVariable::ThreadLocalMode TLM;
471 bool UnnamedAddr;
469472 if (ParseOptionalLinkage(Linkage, HasLinkage) ||
470473 ParseOptionalVisibility(Visibility) ||
471474 ParseOptionalDLLStorageClass(DLLStorageClass) ||
472 ParseOptionalThreadLocal(TLM))
475 ParseOptionalThreadLocal(TLM) ||
476 parseOptionalUnnamedAddr(UnnamedAddr))
473477 return true;
474478
475479 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
476480 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
477 DLLStorageClass, TLM);
478 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass, TLM);
481 DLLStorageClass, TLM, UnnamedAddr);
482 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass, TLM,
483 UnnamedAddr);
479484 }
480485
481486 /// ParseNamedGlobal:
491496 bool HasLinkage;
492497 unsigned Linkage, Visibility, DLLStorageClass;
493498 GlobalVariable::ThreadLocalMode TLM;
499 bool UnnamedAddr;
494500 if (ParseToken(lltok::equal, "expected '=' in global variable") ||
495501 ParseOptionalLinkage(Linkage, HasLinkage) ||
496502 ParseOptionalVisibility(Visibility) ||
497503 ParseOptionalDLLStorageClass(DLLStorageClass) ||
498 ParseOptionalThreadLocal(TLM))
504 ParseOptionalThreadLocal(TLM) ||
505 parseOptionalUnnamedAddr(UnnamedAddr))
499506 return true;
500507
501508 if (HasLinkage || Lex.getKind() != lltok::kw_alias)
502509 return ParseGlobal(Name, NameLoc, Linkage, HasLinkage, Visibility,
503 DLLStorageClass, TLM);
504 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass, TLM);
510 DLLStorageClass, TLM, UnnamedAddr);
511 return ParseAlias(Name, NameLoc, Visibility, DLLStorageClass, TLM,
512 UnnamedAddr);
505513 }
506514
507515 // MDString:
628636
629637 /// ParseAlias:
630638 /// ::= GlobalVar '=' OptionalVisibility OptionalDLLStorageClass
631 /// OptionalThreadLocal 'alias' OptionalLinkage Aliasee
639 /// OptionalThreadLocal OptionalUnNammedAddr 'alias'
640 /// OptionalLinkage Aliasee
632641 ///
633642 /// Aliasee
634643 /// ::= TypeAndValue
635644 ///
636 /// Everything through OptionalThreadLocal has already been parsed.
645 /// Everything through OptionalUnNammedAddr has already been parsed.
637646 ///
638647 bool LLParser::ParseAlias(const std::string &Name, LocTy NameLoc,
639648 unsigned Visibility, unsigned DLLStorageClass,
640 GlobalVariable::ThreadLocalMode TLM) {
649 GlobalVariable::ThreadLocalMode TLM,
650 bool UnnamedAddr) {
641651 assert(Lex.getKind() == lltok::kw_alias);
642652 Lex.Lex();
643653 LocTy LinkageLoc = Lex.getLoc();
686696 GA->setThreadLocalMode(TLM);
687697 GA->setVisibility((GlobalValue::VisibilityTypes)Visibility);
688698 GA->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass);
699 GA->setUnnamedAddr(UnnamedAddr);
689700
690701 // See if this value already exists in the symbol table. If so, it is either
691702 // a redefinition or a definition of a forward reference.
722733
723734 /// ParseGlobal
724735 /// ::= GlobalVar '=' OptionalLinkage OptionalVisibility OptionalDLLStorageClass
725 /// OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
736 /// OptionalThreadLocal OptionalUnNammedAddr OptionalAddrSpace
726737 /// OptionalExternallyInitialized GlobalType Type Const
727738 /// ::= OptionalLinkage OptionalVisibility OptionalDLLStorageClass
728 /// OptionalThreadLocal OptionalAddrSpace OptionalUnNammedAddr
739 /// OptionalThreadLocal OptionalUnNammedAddr OptionalAddrSpace
729740 /// OptionalExternallyInitialized GlobalType Type Const
730741 ///
731 /// Everything up to and including OptionalThreadLocal has been parsed
742 /// Everything up to and including OptionalUnNammedAddr has been parsed
732743 /// already.
733744 ///
734745 bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc,
735746 unsigned Linkage, bool HasLinkage,
736747 unsigned Visibility, unsigned DLLStorageClass,
737 GlobalVariable::ThreadLocalMode TLM) {
748 GlobalVariable::ThreadLocalMode TLM,
749 bool UnnamedAddr) {
738750 if (!isValidVisibilityForLinkage(Visibility, Linkage))
739751 return Error(NameLoc,
740752 "symbol with local linkage must have default visibility");
741753
742754 unsigned AddrSpace;
743 bool IsConstant, UnnamedAddr, IsExternallyInitialized;
744 LocTy UnnamedAddrLoc;
755 bool IsConstant, IsExternallyInitialized;
745756 LocTy IsExternallyInitializedLoc;
746757 LocTy TyLoc;
747758
748759 Type *Ty = nullptr;
749760 if (ParseOptionalAddrSpace(AddrSpace) ||
750 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
751 &UnnamedAddrLoc) ||
752761 ParseOptionalToken(lltok::kw_externally_initialized,
753762 IsExternallyInitialized,
754763 &IsExternallyInitializedLoc) ||
196196
197197 bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
198198 bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
199 bool parseOptionalUnnamedAddr(bool &UnnamedAddr) {
200 return ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr);
201 }
199202 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
200203 bool ParseOptionalParamAttrs(AttrBuilder &B);
201204 bool ParseOptionalReturnAttrs(AttrBuilder &B);
239242 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
240243 bool HasLinkage, unsigned Visibility,
241244 unsigned DLLStorageClass,
242 GlobalVariable::ThreadLocalMode TLM);
245 GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
243246 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility,
244247 unsigned DLLStorageClass,
245 GlobalVariable::ThreadLocalMode TLM);
248 GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
246249 bool ParseStandaloneMetadata();
247250 bool ParseNamedMetadata();
248251 bool ParseMDString(MDString *&Result);
19871987 UpgradeDLLImportExportLinkage(NewGA, Record[2]);
19881988 if (Record.size() > 5)
19891989 NewGA->setThreadLocalMode(GetDecodedThreadLocalMode(Record[5]));
1990 if (Record.size() > 6)
1991 NewGA->setUnnamedAddr(Record[6]);
19901992 ValueList.push_back(NewGA);
19911993 AliasInits.push_back(std::make_pair(NewGA, Record[1]));
19921994 break;
669669 Vals.push_back(getEncodedLinkage(A));
670670 Vals.push_back(getEncodedVisibility(A));
671671 Vals.push_back(getEncodedDLLStorageClass(A));
672 if (A.isThreadLocal())
673 Vals.push_back(getEncodedThreadLocalMode(A));
672 Vals.push_back(getEncodedThreadLocalMode(A));
673 Vals.push_back(A.hasUnnamedAddr());
674674 unsigned AbbrevToUse = 0;
675675 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
676676 Vals.clear();
14501450 PrintVisibility(GV->getVisibility(), Out);
14511451 PrintDLLStorageClass(GV->getDLLStorageClass(), Out);
14521452 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
1453 if (GV->hasUnnamedAddr())
1454 Out << "unnamed_addr ";
14531455
14541456 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
14551457 Out << "addrspace(" << AddressSpace << ") ";
1456 if (GV->hasUnnamedAddr()) Out << "unnamed_addr ";
14571458 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
14581459 Out << (GV->isConstant() ? "constant " : "global ");
14591460 TypePrinter.print(GV->getType()->getElementType(), Out);
14881489 PrintVisibility(GA->getVisibility(), Out);
14891490 PrintDLLStorageClass(GA->getDLLStorageClass(), Out);
14901491 PrintThreadLocalModel(GA->getThreadLocalMode(), Out);
1492 if (GA->hasUnnamedAddr())
1493 Out << "unnamed_addr ";
14911494
14921495 Out << "alias ";
14931496
516516 Assert1(Aliasee, "Aliasee cannot be NULL!", &GA);
517517 Assert1(GA.getType() == Aliasee->getType(),
518518 "Alias and aliasee types should match!", &GA);
519 Assert1(!GA.hasUnnamedAddr(), "Alias cannot have unnamed_addr!", &GA);
520519
521520 Assert1(isa(Aliasee) || isa(Aliasee),
522521 "Aliasee should be either GlobalValue or ConstantExpr", &GA);
892892 bool ModuleLinker::linkAliasProto(GlobalAlias *SGA) {
893893 GlobalValue *DGV = getLinkedToGlobal(SGA);
894894 llvm::Optional NewVisibility;
895 bool HasUnnamedAddr = SGA->hasUnnamedAddr();
895896
896897 if (DGV) {
897898 GlobalValue::LinkageTypes NewLinkage = GlobalValue::InternalLinkage;
900901 if (getLinkageResult(DGV, SGA, NewLinkage, NV, LinkFromSrc))
901902 return true;
902903 NewVisibility = NV;
904 HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
903905
904906 if (!LinkFromSrc) {
905907 // Set calculated linkage.
906908 DGV->setLinkage(NewLinkage);
907909 DGV->setVisibility(*NewVisibility);
910 DGV->setUnnamedAddr(HasUnnamedAddr);
908911
909912 // Make sure to remember this mapping.
910913 ValueMap[SGA] = ConstantExpr::getBitCast(DGV,TypeMap.get(SGA->getType()));
925928 copyGVAttributes(NewDA, SGA);
926929 if (NewVisibility)
927930 NewDA->setVisibility(*NewVisibility);
931 NewDA->setUnnamedAddr(HasUnnamedAddr);
928932
929933 if (DGV) {
930934 // Any uses of DGV need to change to NewDA, with cast.
0 ; RUN: opt < %s -S -nvvm-reflect -nvvm-reflect-list USE_MUL=0 -O2 | FileCheck %s --check-prefix=USE_MUL_0
11 ; RUN: opt < %s -S -nvvm-reflect -nvvm-reflect-list USE_MUL=1 -O2 | FileCheck %s --check-prefix=USE_MUL_1
22
3 @str = private addrspace(4) unnamed_addr constant [8 x i8] c"USE_MUL\00"
3 @str = private unnamed_addr addrspace(4) constant [8 x i8] c"USE_MUL\00"
44
55 declare i32 @__nvvm_reflect(i8*)
66 declare i8* @llvm.nvvm.ptr.constant.to.gen.p0i8.p4i8(i8 addrspace(4)*)
55
66 ; XXX: Test on SI once 64-bit adds are supportes.
77
8 @float_gv = internal addrspace(2) unnamed_addr constant [5 x float] [float 0.0, float 1.0, float 2.0, float 3.0, float 4.0], align 4
8 @float_gv = internal unnamed_addr addrspace(2) constant [5 x float] [float 0.0, float 1.0, float 2.0, float 3.0, float 4.0], align 4
99
1010 ; FUNC-LABEL: @float
1111
2424 ret void
2525 }
2626
27 @i32_gv = internal addrspace(2) unnamed_addr constant [5 x i32] [i32 0, i32 1, i32 2, i32 3, i32 4], align 4
27 @i32_gv = internal unnamed_addr addrspace(2) constant [5 x i32] [i32 0, i32 1, i32 2, i32 3, i32 4], align 4
2828
2929 ; FUNC-LABEL: @i32
3030
4646
4747 %struct.foo = type { float, [5 x i32] }
4848
49 @struct_foo_gv = internal addrspace(2) unnamed_addr constant [1 x %struct.foo] [ %struct.foo { float 16.0, [5 x i32] [i32 0, i32 1, i32 2, i32 3, i32 4] } ]
49 @struct_foo_gv = internal unnamed_addr addrspace(2) constant [1 x %struct.foo] [ %struct.foo { float 16.0, [5 x i32] [i32 0, i32 1, i32 2, i32 3, i32 4] } ]
5050
5151 ; FUNC-LABEL: @struct_foo_gv_load
5252
77 ; CHECK-NOT: ALU clause
88 ; CHECK: MOV * T{{[0-9]\.[XYZW]}}, OQAP
99
10 @local_mem = internal addrspace(3) unnamed_addr global [2 x i32] [i32 1, i32 2], align 4
10 @local_mem = internal unnamed_addr addrspace(3) global [2 x i32] [i32 1, i32 2], align 4
1111
1212 define void @lds_input_queue(i32 addrspace(1)* %out, i32 addrspace(1)* %in, i32 %index) {
1313 entry:
55 ; CHECK-LABEL: @test
66 ; CHECK: .long 166120
77 ; CHECK-NEXT: .long 1
8 @lds = internal addrspace(3) unnamed_addr global i32 zeroinitializer, align 4
8 @lds = internal unnamed_addr addrspace(3) global i32 zeroinitializer, align 4
99
1010 define void @test(i32 addrspace(1)* %out, i32 %cond) {
1111 entry:
0 ; RUN: llc < %s -march=r600 -mcpu=redwood | FileCheck --check-prefix=EG-CHECK %s
11 ; RUN: llc < %s -march=r600 -mcpu=verde -verify-machineinstrs | FileCheck --check-prefix=SI-CHECK %s
22
3 @local_memory_two_objects.local_mem0 = internal addrspace(3) unnamed_addr global [4 x i32] zeroinitializer, align 4
4 @local_memory_two_objects.local_mem1 = internal addrspace(3) unnamed_addr global [4 x i32] zeroinitializer, align 4
3 @local_memory_two_objects.local_mem0 = internal unnamed_addr addrspace(3) global [4 x i32] zeroinitializer, align 4
4 @local_memory_two_objects.local_mem1 = internal unnamed_addr addrspace(3) global [4 x i32] zeroinitializer, align 4
55
66 ; EG-CHECK: @local_memory_two_objects
77
11 ; RUN: llc < %s -march=r600 -mcpu=verde -verify-machineinstrs | FileCheck --check-prefix=SI-CHECK %s
22 ; RUN: llc < %s -march=r600 -mcpu=bonaire -verify-machineinstrs | FileCheck --check-prefix=CI-CHECK %s
33
4 @local_memory.local_mem = internal addrspace(3) unnamed_addr global [128 x i32] zeroinitializer, align 4
4 @local_memory.local_mem = internal unnamed_addr addrspace(3) global [128 x i32] zeroinitializer, align 4
55
66 ; EG-CHECK-LABEL: @local_memory
77 ; SI-CHECK-LABEL: @local_memory
77 @foo1 = alias i32* @bar
88 @foo2 = alias i32* @bar
99 @foo3 = alias i32* @foo2
10 @foo4 = unnamed_addr alias i32* @foo2
1011
1112 %FunTy = type i32()
1213
5151 }
5252
5353
54 @global_as1 = private addrspace(1) unnamed_addr constant [10 x i8] c"ola\00mundo\00", align 1
54 @global_as1 = private unnamed_addr addrspace(1) constant [10 x i8] c"ola\00mundo\00", align 1
5555
5656 define void @f1_as1(i8 addrspace(1)* nocapture %c) {
5757 ; CHECK: @f1_as1
2020 @global-f = weak global i32 42
2121 ; CHECK-DAG: @global-f = global i32 42
2222
23 @alias-a = weak global i32 42
24 ; CHECK-DAG: @alias-a = alias i32* @global-f
25 @alias-b = weak unnamed_addr global i32 42
26 ; CHECK-DAG: @alias-b = unnamed_addr alias i32* @global-f
27
2328 declare void @func-c()
2429 ; CHECK-DAG: define weak void @func-c() {
2530 define weak void @func-d() { ret void }
3742 @global-j = weak global i32 42
3843 ; CHECK-DAG: @global-j = global i32 42
3944
45 @alias-c = weak global i32 42
46 ; CHECK-DAG: @alias-c = alias i32* @global-f
47 @alias-d = weak unnamed_addr global i32 42
48 ; CHECK-DAG: @alias-d = alias i32* @global-f
49
50
4051 declare void @func-g()
4152 ; CHECK-DAG: define weak void @func-g() {
4253 define weak void @func-h() { ret void }
44 @global-d = unnamed_addr global i32 42
55 @global-e = unnamed_addr global i32 42
66 @global-f = unnamed_addr global i32 42
7
8 @alias-a = unnamed_addr alias i32* @global-f
9 @alias-b = unnamed_addr alias i32* @global-f
710
811 define weak void @func-c() unnamed_addr { ret void }
912 define weak void @func-d() unnamed_addr { ret void }
1417 @global-i = global i32 42
1518 @global-j = global i32 42
1619
20 @alias-c = alias i32* @global-f
21 @alias-d = alias i32* @global-f
22
1723 define weak void @func-g() { ret void }
1824 define weak void @func-h() { ret void }
1925 define weak void @func-i() { ret void }
0 ; RUN: opt < %s -instcombine -S | FileCheck %s
11 ; CHECK: addrspacecast
22
3 @base = internal addrspace(3) unnamed_addr global [16 x i32] zeroinitializer, align 16
3 @base = internal unnamed_addr addrspace(3) global [16 x i32] zeroinitializer, align 16
44 declare void @foo(i32*)
55
66 define void @test() nounwind {
4343 EXPECT_TRUE(verifyFunction(*F));
4444 }
4545
46 TEST(VerifierTest, AliasUnnamedAddr) {
47 LLVMContext &C = getGlobalContext();
48 Module M("M", C);
49 Type *Ty = Type::getInt8Ty(C);
50 Constant *Init = Constant::getNullValue(Ty);
51 GlobalVariable *Aliasee = new GlobalVariable(M, Ty, true,
52 GlobalValue::ExternalLinkage,
53 Init, "foo");
54 auto *GA = GlobalAlias::create(GlobalValue::ExternalLinkage, "bar", Aliasee);
55 GA->setUnnamedAddr(true);
56 std::string Error;
57 raw_string_ostream ErrorOS(Error);
58 EXPECT_TRUE(verifyModule(M, &ErrorOS));
59 EXPECT_TRUE(
60 StringRef(ErrorOS.str()).startswith("Alias cannot have unnamed_addr"));
61 }
62
6346 TEST(VerifierTest, InvalidRetAttribute) {
6447 LLVMContext &C = getGlobalContext();
6548 Module M("M", C);