@@ -1034,6 +1034,32 @@ void LLVMToSPIRVBase::transFPGAFunctionMetadata(SPIRVFunction *BF,
1034
1034
}
1035
1035
}
1036
1036
1037
+ SPIRVValue *LLVMToSPIRVBase::transConstantUse (Constant *C) {
1038
+ // Constant expressions expect their pointer types to be i8* in opaque pointer
1039
+ // mode, but the value may have a different "natural" type. If that is the
1040
+ // case, we need to adjust the type of the constant.
1041
+ SPIRVValue *Trans = transValue (C, nullptr , true , FuncTransMode::Pointer);
1042
+ SPIRVType *ExpectedType = transType (C->getType ());
1043
+ if (Trans->getType () == ExpectedType || Trans->getType ()->isTypePipeStorage ())
1044
+ return Trans;
1045
+
1046
+ assert (C->getType ()->isPointerTy () &&
1047
+ " Only pointer type mismatches should be possible" );
1048
+ // In the common case of strings ([N x i8] GVs), see if we can emit a GEP
1049
+ // instruction.
1050
+ if (auto *GV = dyn_cast<GlobalVariable>(C)) {
1051
+ if (GV->getValueType ()->isArrayTy () &&
1052
+ GV->getValueType ()->getArrayElementType ()->isIntegerTy (8 )) {
1053
+ SPIRVValue *Offset = transValue (getUInt32 (M, 0 ), nullptr );
1054
+ return BM->addPtrAccessChainInst (ExpectedType, Trans, {Offset, Offset},
1055
+ nullptr , true );
1056
+ }
1057
+ }
1058
+
1059
+ // Otherwise, just use a bitcast.
1060
+ return BM->addUnaryInst (OpBitcast, ExpectedType, Trans, nullptr );
1061
+ }
1062
+
1037
1063
SPIRVValue *LLVMToSPIRVBase::transConstant (Value *V) {
1038
1064
if (auto CPNull = dyn_cast<ConstantPointerNull>(V))
1039
1065
return BM->addNullConstant (
@@ -1070,30 +1096,28 @@ SPIRVValue *LLVMToSPIRVBase::transConstant(Value *V) {
1070
1096
if (auto ConstDA = dyn_cast<ConstantDataArray>(V)) {
1071
1097
std::vector<SPIRVValue *> BV;
1072
1098
for (unsigned I = 0 , E = ConstDA->getNumElements (); I != E; ++I)
1073
- BV.push_back (transValue (ConstDA->getElementAsConstant (I), nullptr , true ,
1074
- FuncTransMode::Pointer));
1099
+ BV.push_back (transConstantUse (ConstDA->getElementAsConstant (I)));
1075
1100
return BM->addCompositeConstant (transType (V->getType ()), BV);
1076
1101
}
1077
1102
1078
1103
if (auto ConstA = dyn_cast<ConstantArray>(V)) {
1079
1104
std::vector<SPIRVValue *> BV;
1080
1105
for (auto I = ConstA->op_begin (), E = ConstA->op_end (); I != E; ++I)
1081
- BV.push_back (transValue (*I, nullptr , true , FuncTransMode::Pointer ));
1106
+ BV.push_back (transConstantUse (cast<Constant>(*I) ));
1082
1107
return BM->addCompositeConstant (transType (V->getType ()), BV);
1083
1108
}
1084
1109
1085
1110
if (auto ConstDV = dyn_cast<ConstantDataVector>(V)) {
1086
1111
std::vector<SPIRVValue *> BV;
1087
1112
for (unsigned I = 0 , E = ConstDV->getNumElements (); I != E; ++I)
1088
- BV.push_back (transValue (ConstDV->getElementAsConstant (I), nullptr , true ,
1089
- FuncTransMode::Pointer));
1113
+ BV.push_back (transConstantUse (ConstDV->getElementAsConstant (I)));
1090
1114
return BM->addCompositeConstant (transType (V->getType ()), BV);
1091
1115
}
1092
1116
1093
1117
if (auto ConstV = dyn_cast<ConstantVector>(V)) {
1094
1118
std::vector<SPIRVValue *> BV;
1095
1119
for (auto I = ConstV->op_begin (), E = ConstV->op_end (); I != E; ++I)
1096
- BV.push_back (transValue (*I, nullptr , true , FuncTransMode::Pointer ));
1120
+ BV.push_back (transConstantUse (cast<Constant>(*I) ));
1097
1121
return BM->addCompositeConstant (transType (V->getType ()), BV);
1098
1122
}
1099
1123
@@ -1133,7 +1157,7 @@ SPIRVValue *LLVMToSPIRVBase::transConstant(Value *V) {
1133
1157
}
1134
1158
std::vector<SPIRVValue *> BV;
1135
1159
for (auto I = ConstV->op_begin (), E = ConstV->op_end (); I != E; ++I)
1136
- BV.push_back (transValue (*I, nullptr , true , FuncTransMode::Pointer ));
1160
+ BV.push_back (transConstantUse (cast<Constant>(*I) ));
1137
1161
return BM->addCompositeConstant (transType (V->getType ()), BV);
1138
1162
}
1139
1163
0 commit comments