Skip to content

Commit 3b6453e

Browse files
committed
Replace assertions with friendlier checkError() in transMetadataDecorations() (intel#1422)
Original commit: KhronosGroup/SPIRV-LLVM-Translator@1538cf5
1 parent 7854486 commit 3b6453e

File tree

1 file changed

+71
-46
lines changed

1 file changed

+71
-46
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

+71-46
Original file line numberDiff line numberDiff line change
@@ -2069,36 +2069,42 @@ void addFuncPointerCallArgumentAttributes(CallInst *CI,
20692069

20702070
#define ONE_STRING_DECORATION_CASE(NAME, NAMESPACE) \
20712071
case NAMESPACE::Decoration##NAME: { \
2072-
assert(NumOperands == 2 && #NAME " requires exactly 1 extra operand"); \
2072+
ErrLog.checkError(NumOperands == 2, SPIRVEC_InvalidLlvmModule, \
2073+
#NAME " requires exactly 1 extra operand"); \
20732074
auto *StrDecoEO = dyn_cast<MDString>(DecoMD->getOperand(1)); \
2074-
assert(StrDecoEO &&#NAME " requires extra operand to be a string"); \
2075+
ErrLog.checkError(StrDecoEO, SPIRVEC_InvalidLlvmModule, \
2076+
#NAME " requires extra operand to be a string"); \
20752077
Target->addDecorate( \
20762078
new SPIRVDecorate##NAME##Attr(Target, StrDecoEO->getString().str())); \
20772079
break; \
20782080
}
20792081

20802082
#define ONE_INT_DECORATION_CASE(NAME, NAMESPACE, TYPE) \
20812083
case NAMESPACE::Decoration##NAME: { \
2082-
assert(NumOperands == 2 && #NAME " requires exactly 1 extra operand"); \
2084+
ErrLog.checkError(NumOperands == 2, SPIRVEC_InvalidLlvmModule, \
2085+
#NAME " requires exactly 1 extra operand"); \
20832086
auto *IntDecoEO = \
20842087
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1)); \
2085-
assert(IntDecoEO &&#NAME " requires extra operand to be an integer"); \
2088+
ErrLog.checkError(IntDecoEO, SPIRVEC_InvalidLlvmModule, \
2089+
#NAME " requires extra operand to be an integer"); \
20862090
Target->addDecorate(new SPIRVDecorate##NAME( \
20872091
Target, static_cast<TYPE>(IntDecoEO->getZExtValue()))); \
20882092
break; \
20892093
}
20902094

20912095
#define TWO_INT_DECORATION_CASE(NAME, NAMESPACE, TYPE1, TYPE2) \
20922096
case NAMESPACE::Decoration##NAME: { \
2093-
assert(NumOperands == 3 && #NAME " requires exactly 2 extra operand"); \
2097+
ErrLog.checkError(NumOperands == 3, SPIRVEC_InvalidLlvmModule, \
2098+
#NAME " requires exactly 2 extra operands"); \
20942099
auto *IntDecoEO1 = \
20952100
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1)); \
2096-
assert(IntDecoEO1 &&#NAME \
2097-
" requires first extra operand to be an integer"); \
2101+
ErrLog.checkError(IntDecoEO1, SPIRVEC_InvalidLlvmModule, \
2102+
#NAME " requires first extra operand to be an integer"); \
20982103
auto *IntDecoEO2 = \
20992104
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(2)); \
2100-
assert(IntDecoEO2 &&#NAME \
2101-
" requires second extra operand to be an integer"); \
2105+
ErrLog.checkError(IntDecoEO2, SPIRVEC_InvalidLlvmModule, \
2106+
#NAME \
2107+
" requires second extra operand to be an integer"); \
21022108
Target->addDecorate(new SPIRVDecorate##NAME( \
21032109
Target, static_cast<TYPE1>(IntDecoEO1->getZExtValue()), \
21042110
static_cast<TYPE2>(IntDecoEO2->getZExtValue()))); \
@@ -2119,16 +2125,20 @@ void checkIsGlobalVar(SPIRVEntry *E, Decoration Dec) {
21192125
}
21202126

21212127
static void transMetadataDecorations(Metadata *MD, SPIRVEntry *Target) {
2128+
SPIRVErrorLog &ErrLog = Target->getErrorLog();
2129+
21222130
auto *ArgDecoMD = dyn_cast<MDNode>(MD);
21232131
assert(ArgDecoMD && "Decoration list must be a metadata node");
21242132
for (unsigned I = 0, E = ArgDecoMD->getNumOperands(); I != E; ++I) {
21252133
auto *DecoMD = dyn_cast<MDNode>(ArgDecoMD->getOperand(I));
2126-
assert(DecoMD && "Decoration does not name metadata");
2127-
assert(DecoMD->getNumOperands() > 0 &&
2128-
"Decoration metadata must have at least one operand");
2134+
ErrLog.checkError(DecoMD, SPIRVEC_InvalidLlvmModule,
2135+
"Decoration does not name metadata");
2136+
ErrLog.checkError(DecoMD->getNumOperands() > 0, SPIRVEC_InvalidLlvmModule,
2137+
"Decoration metadata must have at least one operand");
21292138
auto *DecoKindConst =
21302139
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(0));
2131-
assert(DecoKindConst && "First operand of decoration must be the kind");
2140+
ErrLog.checkError(DecoKindConst, SPIRVEC_InvalidLlvmModule,
2141+
"First operand of decoration must be the kind");
21322142
auto DecoKind = static_cast<Decoration>(DecoKindConst->getZExtValue());
21332143

21342144
const size_t NumOperands = DecoMD->getNumOperands();
@@ -2155,25 +2165,31 @@ static void transMetadataDecorations(Metadata *MD, SPIRVEntry *Target) {
21552165
break;
21562166
}
21572167
case DecorationMergeINTEL: {
2158-
assert(NumOperands == 3 && "MergeINTEL requires exactly 3 extra operand");
2168+
ErrLog.checkError(NumOperands == 3, SPIRVEC_InvalidLlvmModule,
2169+
"MergeINTEL requires exactly 3 extra operands");
21592170
auto *Name = dyn_cast<MDString>(DecoMD->getOperand(1));
2160-
assert(Name && "MergeINTEL requires first extra operand to be a string");
2171+
ErrLog.checkError(
2172+
Name, SPIRVEC_InvalidLlvmModule,
2173+
"MergeINTEL requires first extra operand to be a string");
21612174
auto *Direction = dyn_cast<MDString>(DecoMD->getOperand(2));
2162-
assert(Direction &&
2163-
"MergeINTEL requires second extra operand to be a string");
2175+
ErrLog.checkError(
2176+
Direction, SPIRVEC_InvalidLlvmModule,
2177+
"MergeINTEL requires second extra operand to be a string");
21642178
Target->addDecorate(new SPIRVDecorateMergeINTELAttr(
21652179
Target, Name->getString().str(), Direction->getString().str()));
21662180
break;
21672181
}
21682182
case DecorationLinkageAttributes: {
2169-
assert(NumOperands == 3 &&
2170-
"LinkageAttributes requires exactly 3 extra operand");
2183+
ErrLog.checkError(NumOperands == 3, SPIRVEC_InvalidLlvmModule,
2184+
"LinkageAttributes requires exactly 3 extra operands");
21712185
auto *Name = dyn_cast<MDString>(DecoMD->getOperand(1));
2172-
assert(Name &&
2173-
"LinkageAttributes requires first extra operand to be a string");
2186+
ErrLog.checkError(
2187+
Name, SPIRVEC_InvalidLlvmModule,
2188+
"LinkageAttributes requires first extra operand to be a string");
21742189
auto *Type = mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(2));
2175-
assert(Type &&
2176-
"LinkageAttributes requires second extra operand to be an int");
2190+
ErrLog.checkError(
2191+
Type, SPIRVEC_InvalidLlvmModule,
2192+
"LinkageAttributes requires second extra operand to be an int");
21772193
auto TypeKind = static_cast<SPIRVLinkageTypeKind>(Type->getZExtValue());
21782194
Target->addDecorate(new SPIRVDecorateLinkageAttr(
21792195
Target, Name->getString().str(), TypeKind));
@@ -2182,44 +2198,49 @@ static void transMetadataDecorations(Metadata *MD, SPIRVEntry *Target) {
21822198
case spv::internal::DecorationHostAccessINTEL: {
21832199
checkIsGlobalVar(Target, DecoKind);
21842200

2185-
assert(NumOperands == 3 && "HostAccessINTEL requires 2 extra operands "
2186-
"after the decoration kind number");
2201+
ErrLog.checkError(NumOperands == 3, SPIRVEC_InvalidLlvmModule,
2202+
"HostAccessINTEL requires exactly 2 extra operands "
2203+
"after the decoration kind number");
21872204
auto *AccessMode =
21882205
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1));
2189-
assert(AccessMode &&
2190-
"HostAccessINTEL requires first extra operand to be an int");
2206+
ErrLog.checkError(
2207+
AccessMode, SPIRVEC_InvalidLlvmModule,
2208+
"HostAccessINTEL requires first extra operand to be an int");
21912209
auto *Name = dyn_cast<MDString>(DecoMD->getOperand(2));
2192-
assert(Name &&
2193-
"HostAccessINTEL requires second extra operand to be a string");
2210+
ErrLog.checkError(
2211+
Name, SPIRVEC_InvalidLlvmModule,
2212+
"HostAccessINTEL requires second extra operand to be a string");
21942213

21952214
Target->addDecorate(new SPIRVDecorateHostAccessINTEL(
21962215
Target, AccessMode->getZExtValue(), Name->getString().str()));
21972216
break;
21982217
}
21992218
case spv::internal::DecorationInitModeINTEL: {
22002219
checkIsGlobalVar(Target, DecoKind);
2201-
assert(static_cast<SPIRVVariable *>(Target)->getInitializer() &&
2202-
"InitModeINTEL only be applied to a global (module scope) "
2203-
"variable which has an Initializer operand");
2220+
ErrLog.checkError(static_cast<SPIRVVariable *>(Target)->getInitializer(),
2221+
SPIRVEC_InvalidLlvmModule,
2222+
"InitModeINTEL only be applied to a global (module "
2223+
"scope) variable which has an Initializer operand");
22042224

2205-
assert(NumOperands == 2 &&
2206-
"InitModeINTEL requires exactly 1 extra operand");
2225+
ErrLog.checkError(NumOperands == 2, SPIRVEC_InvalidLlvmModule,
2226+
"InitModeINTEL requires exactly 1 extra operand");
22072227
auto *Trigger = mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1));
2208-
assert(Trigger &&
2209-
"InitModeINTEL requires extra operand to be an integer");
2228+
ErrLog.checkError(
2229+
Trigger, SPIRVEC_InvalidLlvmModule,
2230+
"InitModeINTEL requires extra operand to be an integer");
22102231

22112232
Target->addDecorate(
22122233
new SPIRVDecorateInitModeINTEL(Target, Trigger->getZExtValue()));
22132234
break;
22142235
}
22152236
case spv::internal::DecorationImplementInCSRINTEL: {
22162237
checkIsGlobalVar(Target, DecoKind);
2217-
2218-
assert(NumOperands == 2 &&
2219-
"ImplementInCSRINTEL requires exactly 1 extra operand");
2238+
ErrLog.checkError(NumOperands == 2, SPIRVEC_InvalidLlvmModule,
2239+
"ImplementInCSRINTEL requires exactly 1 extra operand");
22202240
auto *Value = mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1));
2221-
assert(Value &&
2222-
"ImplementInCSRINTEL requires extra operand to be an integer");
2241+
ErrLog.checkError(
2242+
Value, SPIRVEC_InvalidLlvmModule,
2243+
"ImplementInCSRINTEL requires extra operand to be an integer");
22232244

22242245
Target->addDecorate(
22252246
new SPIRVDecorateImplementInCSRINTEL(Target, Value->getZExtValue()));
@@ -2233,8 +2254,9 @@ static void transMetadataDecorations(Metadata *MD, SPIRVEntry *Target) {
22332254

22342255
auto *DecoValEO1 =
22352256
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1));
2236-
assert(DecoValEO1 &&
2237-
"First extra operand in default decoration case must be integer.");
2257+
ErrLog.checkError(
2258+
DecoValEO1, SPIRVEC_InvalidLlvmModule,
2259+
"First extra operand in default decoration case must be integer.");
22382260
if (NumOperands == 2) {
22392261
Target->addDecorate(
22402262
new SPIRVDecorate(DecoKind, Target, DecoValEO1->getZExtValue()));
@@ -2243,9 +2265,12 @@ static void transMetadataDecorations(Metadata *MD, SPIRVEntry *Target) {
22432265

22442266
auto *DecoValEO2 =
22452267
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(2));
2246-
assert(DecoValEO2 &&
2247-
"First extra operand in default decoration case must be integer.");
2248-
assert(NumOperands == 3 && "At most 2 extra operands expected.");
2268+
ErrLog.checkError(
2269+
DecoValEO2, SPIRVEC_InvalidLlvmModule,
2270+
"Second extra operand in default decoration case must be integer.");
2271+
2272+
ErrLog.checkError(NumOperands == 3, SPIRVEC_InvalidLlvmModule,
2273+
"At most 2 extra operands expected.");
22492274
Target->addDecorate(new SPIRVDecorate(DecoKind, Target,
22502275
DecoValEO1->getZExtValue(),
22512276
DecoValEO2->getZExtValue()));

0 commit comments

Comments
 (0)