Skip to content

Commit 6a51dbd

Browse files
committed
[opaque pointer types] Add an explicit pointee type to alias records in the IR
Since aliases actually use and verify their explicit type already, no further invalid testing is required here. The invalid.test:ALIAS-TYPE-MISMATCH case catches errors due to emitting a non-pointee type in the new format or a non-pointer type in the old format. llvm-svn: 247952
1 parent bf19a11 commit 6a51dbd

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ namespace bitc {
6969
MODULE_CODE_FUNCTION = 8,
7070

7171
// ALIAS: [alias type, aliasee val#, linkage, visibility]
72-
MODULE_CODE_ALIAS = 9,
72+
MODULE_CODE_ALIAS_OLD = 9,
7373

7474
// MODULE_CODE_PURGEVALS: [numvals]
7575
MODULE_CODE_PURGEVALS = 10,
@@ -78,6 +78,9 @@ namespace bitc {
7878
MODULE_CODE_COMDAT = 12, // COMDAT: [selection_kind, name]
7979

8080
MODULE_CODE_VSTOFFSET = 13, // VSTOFFSET: [offset]
81+
82+
// ALIAS: [alias value type, addrspace, aliasee val#, linkage, visibility]
83+
MODULE_CODE_ALIAS = 14,
8184
};
8285

8386
/// PARAMATTR blocks have code for defining a parameter attribute set.

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3029,7 +3029,8 @@ std::error_code BitcodeReader::parseModule(bool Resume,
30293029

30303030

30313031
// Read a record.
3032-
switch (Stream.readRecord(Entry.ID, Record)) {
3032+
auto BitCode = Stream.readRecord(Entry.ID, Record);
3033+
switch (BitCode) {
30333034
default: break; // Default behavior, ignore unknown content.
30343035
case bitc::MODULE_CODE_VERSION: { // VERSION: [version#]
30353036
if (Record.size() < 1)
@@ -3268,36 +3269,51 @@ std::error_code BitcodeReader::parseModule(bool Resume,
32683269
}
32693270
break;
32703271
}
3271-
// ALIAS: [alias type, aliasee val#, linkage]
3272-
// ALIAS: [alias type, aliasee val#, linkage, visibility, dllstorageclass]
3273-
case bitc::MODULE_CODE_ALIAS: {
3274-
if (Record.size() < 3)
3272+
// ALIAS: [alias type, addrspace, aliasee val#, linkage]
3273+
// ALIAS: [alias type, addrspace, aliasee val#, linkage, visibility, dllstorageclass]
3274+
case bitc::MODULE_CODE_ALIAS:
3275+
case bitc::MODULE_CODE_ALIAS_OLD: {
3276+
bool NewRecord = BitCode == bitc::MODULE_CODE_ALIAS;
3277+
if (Record.size() < (3 + NewRecord))
32753278
return error("Invalid record");
3276-
Type *Ty = getTypeByID(Record[0]);
3279+
unsigned OpNum = 0;
3280+
Type *Ty = getTypeByID(Record[OpNum++]);
32773281
if (!Ty)
32783282
return error("Invalid record");
3279-
auto *PTy = dyn_cast<PointerType>(Ty);
3280-
if (!PTy)
3281-
return error("Invalid type for value");
32823283

3283-
auto *NewGA =
3284-
GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
3285-
getDecodedLinkage(Record[2]), "", TheModule);
3284+
unsigned AddrSpace;
3285+
if (!NewRecord) {
3286+
auto *PTy = dyn_cast<PointerType>(Ty);
3287+
if (!PTy)
3288+
return error("Invalid type for value");
3289+
Ty = PTy->getElementType();
3290+
AddrSpace = PTy->getAddressSpace();
3291+
} else {
3292+
AddrSpace = Record[OpNum++];
3293+
}
3294+
3295+
auto Val = Record[OpNum++];
3296+
auto Linkage = Record[OpNum++];
3297+
auto *NewGA = GlobalAlias::create(
3298+
Ty, AddrSpace, getDecodedLinkage(Linkage), "", TheModule);
32863299
// Old bitcode files didn't have visibility field.
32873300
// Local linkage must have default visibility.
3288-
if (Record.size() > 3 && !NewGA->hasLocalLinkage())
3289-
// FIXME: Change to an error if non-default in 4.0.
3290-
NewGA->setVisibility(getDecodedVisibility(Record[3]));
3291-
if (Record.size() > 4)
3292-
NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[4]));
3301+
if (OpNum != Record.size()) {
3302+
auto VisInd = OpNum++;
3303+
if (!NewGA->hasLocalLinkage())
3304+
// FIXME: Change to an error if non-default in 4.0.
3305+
NewGA->setVisibility(getDecodedVisibility(Record[VisInd]));
3306+
}
3307+
if (OpNum != Record.size())
3308+
NewGA->setDLLStorageClass(getDecodedDLLStorageClass(Record[OpNum++]));
32933309
else
3294-
upgradeDLLImportExportLinkage(NewGA, Record[2]);
3295-
if (Record.size() > 5)
3296-
NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[5]));
3297-
if (Record.size() > 6)
3298-
NewGA->setUnnamedAddr(Record[6]);
3310+
upgradeDLLImportExportLinkage(NewGA, Linkage);
3311+
if (OpNum != Record.size())
3312+
NewGA->setThreadLocalMode(getDecodedThreadLocalMode(Record[OpNum++]));
3313+
if (OpNum != Record.size())
3314+
NewGA->setUnnamedAddr(Record[OpNum++]);
32993315
ValueList.push_back(NewGA);
3300-
AliasInits.push_back(std::make_pair(NewGA, Record[1]));
3316+
AliasInits.push_back(std::make_pair(NewGA, Val));
33013317
break;
33023318
}
33033319
/// MODULE_CODE_PURGEVALS: [numvals]

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,8 @@ static uint64_t WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
756756
// Emit the alias information.
757757
for (const GlobalAlias &A : M->aliases()) {
758758
// ALIAS: [alias type, aliasee val#, linkage, visibility]
759-
Vals.push_back(VE.getTypeID(A.getType()));
759+
Vals.push_back(VE.getTypeID(A.getValueType()));
760+
Vals.push_back(A.getType()->getAddressSpace());
760761
Vals.push_back(VE.getValueID(A.getAliasee()));
761762
Vals.push_back(getEncodedLinkage(A));
762763
Vals.push_back(getEncodedVisibility(A));

0 commit comments

Comments
 (0)