@@ -970,7 +970,7 @@ bool LLParser::parseIndirectSymbol(const std::string &Name, LocTy NameLoc,
970
970
} else {
971
971
// The bitcast dest type is not present, it is implied by the dest type.
972
972
ValID ID;
973
- if (parseValID (ID))
973
+ if (parseValID (ID, /* PFS= */ nullptr ))
974
974
return true ;
975
975
if (ID.Kind != ValID::t_Constant)
976
976
return error (AliaseeLoc, " invalid aliasee" );
@@ -3321,7 +3321,7 @@ BasicBlock *LLParser::PerFunctionState::defineBB(const std::string &Name,
3321
3321
// / sanity. PFS is used to convert function-local operands of metadata (since
3322
3322
// / metadata operands are not just parsed here but also converted to values).
3323
3323
// / PFS can be null when we are not parsing metadata values inside a function.
3324
- bool LLParser::parseValID (ValID &ID, PerFunctionState *PFS) {
3324
+ bool LLParser::parseValID (ValID &ID, PerFunctionState *PFS, Type *ExpectedTy ) {
3325
3325
ID.Loc = Lex.getLoc ();
3326
3326
switch (Lex.getKind ()) {
3327
3327
default :
@@ -3493,10 +3493,10 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS) {
3493
3493
ValID Fn, Label;
3494
3494
3495
3495
if (parseToken (lltok::lparen, " expected '(' in block address expression" ) ||
3496
- parseValID (Fn) ||
3496
+ parseValID (Fn, PFS ) ||
3497
3497
parseToken (lltok::comma,
3498
3498
" expected comma in block address expression" ) ||
3499
- parseValID (Label) ||
3499
+ parseValID (Label, PFS ) ||
3500
3500
parseToken (lltok::rparen, " expected ')' in block address expression" ))
3501
3501
return true ;
3502
3502
@@ -3531,9 +3531,27 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS) {
3531
3531
std::map<ValID, GlobalValue *>()))
3532
3532
.first ->second .insert (std::make_pair (std::move (Label), nullptr ))
3533
3533
.first ->second ;
3534
- if (!FwdRef)
3535
- FwdRef = new GlobalVariable (*M, Type::getInt8Ty (Context), false ,
3536
- GlobalValue::InternalLinkage, nullptr , " " );
3534
+ if (!FwdRef) {
3535
+ unsigned FwdDeclAS;
3536
+ if (ExpectedTy) {
3537
+ // If we know the type that the blockaddress is being assigned to,
3538
+ // we can use the address space of that type.
3539
+ if (!ExpectedTy->isPointerTy ())
3540
+ return error (ID.Loc ,
3541
+ " type of blockaddress must be a pointer and not '" +
3542
+ getTypeString (ExpectedTy) + " '" );
3543
+ FwdDeclAS = ExpectedTy->getPointerAddressSpace ();
3544
+ } else if (PFS) {
3545
+ // Otherwise, we default the address space of the current function.
3546
+ FwdDeclAS = PFS->getFunction ().getAddressSpace ();
3547
+ } else {
3548
+ llvm_unreachable (" Unknown address space for blockaddress" );
3549
+ }
3550
+ FwdRef = new GlobalVariable (
3551
+ *M, Type::getInt8Ty (Context), false , GlobalValue::InternalLinkage,
3552
+ nullptr , " " , nullptr , GlobalValue::NotThreadLocal, FwdDeclAS);
3553
+ }
3554
+
3537
3555
ID.ConstantVal = FwdRef;
3538
3556
ID.Kind = ValID::t_Constant;
3539
3557
return false ;
@@ -3570,7 +3588,7 @@ bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS) {
3570
3588
3571
3589
ValID Fn;
3572
3590
3573
- if (parseValID (Fn))
3591
+ if (parseValID (Fn, PFS ))
3574
3592
return true ;
3575
3593
3576
3594
if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
@@ -3960,7 +3978,7 @@ bool LLParser::parseGlobalValue(Type *Ty, Constant *&C) {
3960
3978
C = nullptr ;
3961
3979
ValID ID;
3962
3980
Value *V = nullptr ;
3963
- bool Parsed = parseValID (ID) ||
3981
+ bool Parsed = parseValID (ID, /* PFS= */ nullptr , Ty ) ||
3964
3982
convertValIDToValue (Ty, ID, V, nullptr , /* IsCall=*/ false );
3965
3983
if (V && !(C = dyn_cast<Constant>(V)))
3966
3984
return error (ID.Loc , " global values must be constants" );
@@ -5679,7 +5697,9 @@ bool LLParser::convertValIDToValue(Type *Ty, ValID &ID, Value *&V,
5679
5697
return false ;
5680
5698
case ValID::t_Constant:
5681
5699
if (ID.ConstantVal ->getType () != Ty)
5682
- return error (ID.Loc , " constant expression type mismatch" );
5700
+ return error (ID.Loc , " constant expression type mismatch: got type '" +
5701
+ getTypeString (ID.ConstantVal ->getType ()) +
5702
+ " ' but expected '" + getTypeString (Ty) + " '" );
5683
5703
V = ID.ConstantVal ;
5684
5704
return false ;
5685
5705
case ValID::t_ConstantStruct:
@@ -5739,7 +5759,7 @@ bool LLParser::parseConstantValue(Type *Ty, Constant *&C) {
5739
5759
bool LLParser::parseValue (Type *Ty, Value *&V, PerFunctionState *PFS) {
5740
5760
V = nullptr ;
5741
5761
ValID ID;
5742
- return parseValID (ID, PFS) ||
5762
+ return parseValID (ID, PFS, Ty ) ||
5743
5763
convertValIDToValue (Ty, ID, V, PFS, /* IsCall=*/ false );
5744
5764
}
5745
5765
@@ -6033,7 +6053,12 @@ bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() {
6033
6053
if (!BB)
6034
6054
return P.error (BBID.Loc , " referenced value is not a basic block" );
6035
6055
6036
- GV->replaceAllUsesWith (BlockAddress::get (&F, BB));
6056
+ Value *ResolvedVal = BlockAddress::get (&F, BB);
6057
+ ResolvedVal = P.checkValidVariableType (BBID.Loc , BBID.StrVal , GV->getType (),
6058
+ ResolvedVal, false );
6059
+ if (!ResolvedVal)
6060
+ return true ;
6061
+ GV->replaceAllUsesWith (ResolvedVal);
6037
6062
GV->eraseFromParent ();
6038
6063
}
6039
6064
@@ -6568,7 +6593,7 @@ bool LLParser::parseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
6568
6593
if (parseOptionalCallingConv (CC) || parseOptionalReturnAttrs (RetAttrs) ||
6569
6594
parseOptionalProgramAddrSpace (InvokeAddrSpace) ||
6570
6595
parseType (RetType, RetTypeLoc, true /* void allowed*/ ) ||
6571
- parseValID (CalleeID) || parseParameterList (ArgList, PFS) ||
6596
+ parseValID (CalleeID, &PFS ) || parseParameterList (ArgList, PFS) ||
6572
6597
parseFnAttributeValuePairs (FnAttrs, FwdRefAttrGrps, false ,
6573
6598
NoBuiltinLoc) ||
6574
6599
parseOptionalOperandBundles (BundleList, PFS) ||
@@ -6876,7 +6901,7 @@ bool LLParser::parseCallBr(Instruction *&Inst, PerFunctionState &PFS) {
6876
6901
BasicBlock *DefaultDest;
6877
6902
if (parseOptionalCallingConv (CC) || parseOptionalReturnAttrs (RetAttrs) ||
6878
6903
parseType (RetType, RetTypeLoc, true /* void allowed*/ ) ||
6879
- parseValID (CalleeID) || parseParameterList (ArgList, PFS) ||
6904
+ parseValID (CalleeID, &PFS ) || parseParameterList (ArgList, PFS) ||
6880
6905
parseFnAttributeValuePairs (FnAttrs, FwdRefAttrGrps, false ,
6881
6906
NoBuiltinLoc) ||
6882
6907
parseOptionalOperandBundles (BundleList, PFS) ||
@@ -7303,7 +7328,7 @@ bool LLParser::parseCall(Instruction *&Inst, PerFunctionState &PFS,
7303
7328
if (parseOptionalCallingConv (CC) || parseOptionalReturnAttrs (RetAttrs) ||
7304
7329
parseOptionalProgramAddrSpace (CallAddrSpace) ||
7305
7330
parseType (RetType, RetTypeLoc, true /* void allowed*/ ) ||
7306
- parseValID (CalleeID) ||
7331
+ parseValID (CalleeID, &PFS ) ||
7307
7332
parseParameterList (ArgList, PFS, TCK == CallInst::TCK_MustTail,
7308
7333
PFS.getFunction ().isVarArg ()) ||
7309
7334
parseFnAttributeValuePairs (FnAttrs, FwdRefAttrGrps, false , BuiltinLoc) ||
@@ -7979,9 +8004,9 @@ bool LLParser::parseUseListOrderBB() {
7979
8004
7980
8005
ValID Fn, Label;
7981
8006
SmallVector<unsigned , 16 > Indexes;
7982
- if (parseValID (Fn) ||
8007
+ if (parseValID (Fn, /* PFS= */ nullptr ) ||
7983
8008
parseToken (lltok::comma, " expected comma in uselistorder_bb directive" ) ||
7984
- parseValID (Label) ||
8009
+ parseValID (Label, /* PFS= */ nullptr ) ||
7985
8010
parseToken (lltok::comma, " expected comma in uselistorder_bb directive" ) ||
7986
8011
parseUseListOrderIndexes (Indexes))
7987
8012
return true ;
0 commit comments