Skip to content

Commit d291f77

Browse files
authored
[ESIMD] Fix potential accesses on nullptr reported by static verifier (#6383)
Some places in LowerESIMD pass had assert statements preventing from access data referenced by nullptr. Those places were reported by static verfier for RELEASE build because 'assert' is NOP there. The fix calls llvm::report_fatal_error if the condition is not met. Also, the fix adds a missing copyright comment section to one of files created reacently. Signed-off-by: Vyacheslav N Klochkov <[email protected]>
1 parent eab2969 commit d291f77

File tree

3 files changed

+55
-43
lines changed

3 files changed

+55
-43
lines changed

llvm/include/llvm/SYCLLowerIR/ESIMD/ESIMDUtils.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===----------- ESIMDUtils.hpp - ESIMD t-forms-related utility functions ===//
1+
//===------------ ESIMDUtils.h - ESIMD utility functions ------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -35,5 +35,15 @@ void traverseCallgraphUp(Function *F, CallGraphNodeActionF ActionF,
3535
// Tells whether given function is a ESIMD kernel.
3636
bool isESIMDKernel(const Function &F);
3737

38+
/// Reports and error with the message \p Msg concatenated with the optional
39+
/// \p OptMsg if \p Condition is false.
40+
inline void assert_and_diag(bool Condition, StringRef Msg,
41+
StringRef OptMsg = "") {
42+
if (!Condition) {
43+
auto T = Twine(Msg) + OptMsg;
44+
llvm::report_fatal_error(T, true /* crash diagnostics */);
45+
}
46+
}
47+
3848
} // namespace esimd
3949
} // namespace llvm

llvm/lib/SYCLLowerIR/ESIMD/ESIMDUtils.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
//===------------ ESIMDUtils.cpp - ESIMD utility functions ----------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// Utility functions for processing ESIMD code.
9+
//===----------------------------------------------------------------------===//
10+
111
#include "llvm/SYCLLowerIR/ESIMD/ESIMDUtils.h"
212

313
#include "llvm/ADT/SmallPtrSet.h"

llvm/lib/SYCLLowerIR/ESIMD/LowerESIMD.cpp

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -671,10 +671,8 @@ static const ESIMDIntrinDesc &getIntrinDesc(StringRef SrcSpelling) {
671671
const auto &Table = getIntrinTable();
672672
auto It = Table.find(SrcSpelling.str());
673673

674-
if (It == Table.end()) {
675-
Twine Msg("unknown ESIMD intrinsic: " + SrcSpelling);
676-
llvm::report_fatal_error(Msg, false /*no crash diag*/);
677-
}
674+
llvm::esimd::assert_and_diag(It != Table.end(),
675+
"unknown ESIMD intrinsic: ", SrcSpelling);
678676
return It->second;
679677
}
680678

@@ -926,7 +924,7 @@ struct UpdateUint64MetaDataToMaxValue {
926924
: M(M), Key(Key), NewVal(NewVal) {
927925
// Pre-select nodes for update to do less work in the '()' operator.
928926
llvm::NamedMDNode *GenXKernelMD = M.getNamedMetadata(GENX_KERNEL_METADATA);
929-
assert(GenXKernelMD && "invalid genx.kernels metadata");
927+
llvm::esimd::assert_and_diag(GenXKernelMD, "invalid genx.kernels metadata");
930928
for (auto Node : GenXKernelMD->operands()) {
931929
if (Node->getNumOperands() <= (unsigned)Key) {
932930
continue;
@@ -969,9 +967,8 @@ struct UpdateUint64MetaDataToMaxValue {
969967
static void translateSLMInit(CallInst &CI) {
970968
auto F = CI.getFunction();
971969
auto *ArgV = CI.getArgOperand(0);
972-
if (!isa<ConstantInt>(ArgV))
973-
llvm::report_fatal_error(llvm::Twine(__FILE__ " ") +
974-
"integral constant is expected for slm size");
970+
llvm::esimd::assert_and_diag(isa<ConstantInt>(ArgV), __FILE__,
971+
" integral constant is expected for slm size");
975972

976973
uint64_t NewVal = cast<llvm::ConstantInt>(ArgV)->getZExtValue();
977974
assert(NewVal != 0 && "zero slm bytes being requested");
@@ -985,10 +982,9 @@ static void translateSLMInit(CallInst &CI) {
985982
static void translateNbarrierInit(CallInst &CI) {
986983
auto F = CI.getFunction();
987984
auto *ArgV = CI.getArgOperand(0);
988-
if (!isa<ConstantInt>(ArgV))
989-
llvm::report_fatal_error(
990-
llvm::Twine(__FILE__ " ") +
991-
"integral constant is expected for named barrier count");
985+
llvm::esimd::assert_and_diag(
986+
isa<ConstantInt>(ArgV), __FILE__,
987+
" integral constant is expected for named barrier count");
992988

993989
auto NewVal = cast<llvm::ConstantInt>(ArgV)->getZExtValue();
994990
assert(NewVal != 0 && "zero named barrier count being requested");
@@ -1000,20 +996,18 @@ static void translateNbarrierInit(CallInst &CI) {
1000996
static void translatePackMask(CallInst &CI) {
1001997
using Demangler = id::ManglingParser<SimpleAllocator>;
1002998
Function *F = CI.getCalledFunction();
1003-
assert(F && "function to translate is invalid");
999+
llvm::esimd::assert_and_diag(F, "function to translate is invalid");
10041000

10051001
StringRef MnglName = F->getName();
10061002
Demangler Parser(MnglName.begin(), MnglName.end());
10071003
id::Node *AST = Parser.parse();
10081004

1009-
if (!AST || !Parser.ForwardTemplateRefs.empty()) {
1010-
Twine Msg("failed to demangle ESIMD intrinsic: " + MnglName);
1011-
llvm::report_fatal_error(Msg, false /*no crash diag*/);
1012-
}
1013-
if (AST->getKind() != id::Node::KFunctionEncoding) {
1014-
Twine Msg("bad ESIMD intrinsic: " + MnglName);
1015-
llvm::report_fatal_error(Msg, false /*no crash diag*/);
1016-
}
1005+
llvm::esimd::assert_and_diag(
1006+
AST && Parser.ForwardTemplateRefs.empty(),
1007+
"failed to demangle ESIMD intrinsic: ", MnglName);
1008+
llvm::esimd::assert_and_diag(AST->getKind() == id::Node::KFunctionEncoding,
1009+
"bad ESIMD intrinsic: ", MnglName);
1010+
10171011
auto *FE = static_cast<id::FunctionEncoding *>(AST);
10181012
llvm::LLVMContext &Context = CI.getContext();
10191013
Type *TTy = nullptr;
@@ -1042,19 +1036,17 @@ static void translatePackMask(CallInst &CI) {
10421036
static void translateUnPackMask(CallInst &CI) {
10431037
using Demangler = id::ManglingParser<SimpleAllocator>;
10441038
Function *F = CI.getCalledFunction();
1045-
assert(F && "function to translate is invalid");
1039+
llvm::esimd::assert_and_diag(F, "function to translate is invalid");
10461040
StringRef MnglName = F->getName();
10471041
Demangler Parser(MnglName.begin(), MnglName.end());
10481042
id::Node *AST = Parser.parse();
10491043

1050-
if (!AST || !Parser.ForwardTemplateRefs.empty()) {
1051-
Twine Msg("failed to demangle ESIMD intrinsic: " + MnglName);
1052-
llvm::report_fatal_error(Msg, false /*no crash diag*/);
1053-
}
1054-
if (AST->getKind() != id::Node::KFunctionEncoding) {
1055-
Twine Msg("bad ESIMD intrinsic: " + MnglName);
1056-
llvm::report_fatal_error(Msg, false /*no crash diag*/);
1057-
}
1044+
llvm::esimd::assert_and_diag(
1045+
AST && Parser.ForwardTemplateRefs.empty(),
1046+
"failed to demangle ESIMD intrinsic: ", MnglName);
1047+
llvm::esimd::assert_and_diag(AST->getKind() == id::Node::KFunctionEncoding,
1048+
"bad ESIMD intrinsic: ", MnglName);
1049+
10581050
auto *FE = static_cast<id::FunctionEncoding *>(AST);
10591051
llvm::LLVMContext &Context = CI.getContext();
10601052
Type *TTy = nullptr;
@@ -1194,8 +1186,9 @@ void translateFmuladd(CallInst *CI) {
11941186

11951187
// Translates an LLVM intrinsic to a form, digestable by the BE.
11961188
bool translateLLVMIntrinsic(CallInst *CI) {
1197-
Function *F = CI->getCalledFunction() ? CI->getCalledFunction() : nullptr;
1198-
assert(F && F->isIntrinsic());
1189+
Function *F = CI->getCalledFunction();
1190+
llvm::esimd::assert_and_diag(F && F->isIntrinsic(),
1191+
"malformed llvm intrinsic call");
11991192

12001193
switch (F->getIntrinsicID()) {
12011194
case Intrinsic::assume:
@@ -1277,7 +1270,8 @@ translateSpirvGlobalUses(LoadInst *LI, StringRef SpirvGlobalName,
12771270
NewInst = generateGenXCall(EEI, "group.count", true);
12781271
}
12791272

1280-
assert(NewInst && "Load from global SPIRV builtin was not translated");
1273+
llvm::esimd::assert_and_diag(
1274+
NewInst, "Load from global SPIRV builtin was not translated");
12811275
EEI->replaceAllUsesWith(NewInst);
12821276
InstsToErase.push_back(EEI);
12831277
}
@@ -1437,19 +1431,17 @@ static Function *createTestESIMDDeclaration(const ESIMDIntrinDesc &Desc,
14371431
static void translateESIMDIntrinsicCall(CallInst &CI) {
14381432
using Demangler = id::ManglingParser<SimpleAllocator>;
14391433
Function *F = CI.getCalledFunction();
1440-
assert(F && "function to translate is invalid");
1434+
llvm::esimd::assert_and_diag(F, "function to translate is invalid");
14411435
StringRef MnglName = F->getName();
14421436
Demangler Parser(MnglName.begin(), MnglName.end());
14431437
id::Node *AST = Parser.parse();
14441438

1445-
if (!AST || !Parser.ForwardTemplateRefs.empty()) {
1446-
Twine Msg("failed to demangle ESIMD intrinsic: " + MnglName);
1447-
llvm::report_fatal_error(Msg, false /*no crash diag*/);
1448-
}
1449-
if (AST->getKind() != id::Node::KFunctionEncoding) {
1450-
Twine Msg("bad ESIMD intrinsic: " + MnglName);
1451-
llvm::report_fatal_error(Msg, false /*no crash diag*/);
1452-
}
1439+
llvm::esimd::assert_and_diag(
1440+
AST && Parser.ForwardTemplateRefs.empty(),
1441+
"failed to demangle ESIMD intrinsic: ", MnglName);
1442+
llvm::esimd::assert_and_diag(AST->getKind() == id::Node::KFunctionEncoding,
1443+
"bad ESIMD intrinsic: ", MnglName);
1444+
14531445
auto *FE = static_cast<id::FunctionEncoding *>(AST);
14541446
id::StringView BaseNameV = FE->getName()->getBaseName();
14551447

0 commit comments

Comments
 (0)