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