Skip to content

[ESIMD] Fix potential accesses on nullptr reported by static verifier #6383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion llvm/include/llvm/SYCLLowerIR/ESIMD/ESIMDUtils.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===----------- ESIMDUtils.hpp - ESIMD t-forms-related utility functions ===//
//===------------ ESIMDUtils.h - ESIMD utility functions ------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
Expand Down Expand Up @@ -35,5 +35,15 @@ void traverseCallgraphUp(Function *F, CallGraphNodeActionF ActionF,
// Tells whether given function is a ESIMD kernel.
bool isESIMDKernel(const Function &F);

/// Reports and error with the message \p Msg concatenated with the optional
/// \p OptMsg if \p Condition is false.
inline void assert_and_diag(bool Condition, StringRef Msg,
StringRef OptMsg = "") {
if (!Condition) {
auto T = Twine(Msg) + OptMsg;
llvm::report_fatal_error(T, true /* crash diagnostics */);
}
}

} // namespace esimd
} // namespace llvm
10 changes: 10 additions & 0 deletions llvm/lib/SYCLLowerIR/ESIMD/ESIMDUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
//===------------ ESIMDUtils.cpp - ESIMD utility functions ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// Utility functions for processing ESIMD code.
//===----------------------------------------------------------------------===//

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

#include "llvm/ADT/SmallPtrSet.h"
Expand Down
76 changes: 34 additions & 42 deletions llvm/lib/SYCLLowerIR/ESIMD/LowerESIMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,10 +671,8 @@ static const ESIMDIntrinDesc &getIntrinDesc(StringRef SrcSpelling) {
const auto &Table = getIntrinTable();
auto It = Table.find(SrcSpelling.str());

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

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

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

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

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

if (!AST || !Parser.ForwardTemplateRefs.empty()) {
Twine Msg("failed to demangle ESIMD intrinsic: " + MnglName);
llvm::report_fatal_error(Msg, false /*no crash diag*/);
}
if (AST->getKind() != id::Node::KFunctionEncoding) {
Twine Msg("bad ESIMD intrinsic: " + MnglName);
llvm::report_fatal_error(Msg, false /*no crash diag*/);
}
llvm::esimd::assert_and_diag(
AST && Parser.ForwardTemplateRefs.empty(),
"failed to demangle ESIMD intrinsic: ", MnglName);
llvm::esimd::assert_and_diag(AST->getKind() == id::Node::KFunctionEncoding,
"bad ESIMD intrinsic: ", MnglName);

auto *FE = static_cast<id::FunctionEncoding *>(AST);
llvm::LLVMContext &Context = CI.getContext();
Type *TTy = nullptr;
Expand Down Expand Up @@ -1042,19 +1036,17 @@ static void translatePackMask(CallInst &CI) {
static void translateUnPackMask(CallInst &CI) {
using Demangler = id::ManglingParser<SimpleAllocator>;
Function *F = CI.getCalledFunction();
assert(F && "function to translate is invalid");
llvm::esimd::assert_and_diag(F, "function to translate is invalid");
StringRef MnglName = F->getName();
Demangler Parser(MnglName.begin(), MnglName.end());
id::Node *AST = Parser.parse();

if (!AST || !Parser.ForwardTemplateRefs.empty()) {
Twine Msg("failed to demangle ESIMD intrinsic: " + MnglName);
llvm::report_fatal_error(Msg, false /*no crash diag*/);
}
if (AST->getKind() != id::Node::KFunctionEncoding) {
Twine Msg("bad ESIMD intrinsic: " + MnglName);
llvm::report_fatal_error(Msg, false /*no crash diag*/);
}
llvm::esimd::assert_and_diag(
AST && Parser.ForwardTemplateRefs.empty(),
"failed to demangle ESIMD intrinsic: ", MnglName);
llvm::esimd::assert_and_diag(AST->getKind() == id::Node::KFunctionEncoding,
"bad ESIMD intrinsic: ", MnglName);

auto *FE = static_cast<id::FunctionEncoding *>(AST);
llvm::LLVMContext &Context = CI.getContext();
Type *TTy = nullptr;
Expand Down Expand Up @@ -1194,8 +1186,9 @@ void translateFmuladd(CallInst *CI) {

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

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

assert(NewInst && "Load from global SPIRV builtin was not translated");
llvm::esimd::assert_and_diag(
NewInst, "Load from global SPIRV builtin was not translated");
EEI->replaceAllUsesWith(NewInst);
InstsToErase.push_back(EEI);
}
Expand Down Expand Up @@ -1437,19 +1431,17 @@ static Function *createTestESIMDDeclaration(const ESIMDIntrinDesc &Desc,
static void translateESIMDIntrinsicCall(CallInst &CI) {
using Demangler = id::ManglingParser<SimpleAllocator>;
Function *F = CI.getCalledFunction();
assert(F && "function to translate is invalid");
llvm::esimd::assert_and_diag(F, "function to translate is invalid");
StringRef MnglName = F->getName();
Demangler Parser(MnglName.begin(), MnglName.end());
id::Node *AST = Parser.parse();

if (!AST || !Parser.ForwardTemplateRefs.empty()) {
Twine Msg("failed to demangle ESIMD intrinsic: " + MnglName);
llvm::report_fatal_error(Msg, false /*no crash diag*/);
}
if (AST->getKind() != id::Node::KFunctionEncoding) {
Twine Msg("bad ESIMD intrinsic: " + MnglName);
llvm::report_fatal_error(Msg, false /*no crash diag*/);
}
llvm::esimd::assert_and_diag(
AST && Parser.ForwardTemplateRefs.empty(),
"failed to demangle ESIMD intrinsic: ", MnglName);
llvm::esimd::assert_and_diag(AST->getKind() == id::Node::KFunctionEncoding,
"bad ESIMD intrinsic: ", MnglName);

auto *FE = static_cast<id::FunctionEncoding *>(AST);
id::StringView BaseNameV = FE->getName()->getBaseName();

Expand Down