diff --git a/llvm/include/llvm/SYCLLowerIR/ESIMD/ESIMDUtils.h b/llvm/include/llvm/SYCLLowerIR/ESIMD/ESIMDUtils.h index 17c4d952a6189..109753fb3b3a1 100644 --- a/llvm/include/llvm/SYCLLowerIR/ESIMD/ESIMDUtils.h +++ b/llvm/include/llvm/SYCLLowerIR/ESIMD/ESIMDUtils.h @@ -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. @@ -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 diff --git a/llvm/lib/SYCLLowerIR/ESIMD/ESIMDUtils.cpp b/llvm/lib/SYCLLowerIR/ESIMD/ESIMDUtils.cpp index 8345dc5be0417..a2ddc06d1fa06 100644 --- a/llvm/lib/SYCLLowerIR/ESIMD/ESIMDUtils.cpp +++ b/llvm/lib/SYCLLowerIR/ESIMD/ESIMDUtils.cpp @@ -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" diff --git a/llvm/lib/SYCLLowerIR/ESIMD/LowerESIMD.cpp b/llvm/lib/SYCLLowerIR/ESIMD/LowerESIMD.cpp index 9580bee454a27..b3b337e0048aa 100644 --- a/llvm/lib/SYCLLowerIR/ESIMD/LowerESIMD.cpp +++ b/llvm/lib/SYCLLowerIR/ESIMD/LowerESIMD.cpp @@ -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; } @@ -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; @@ -969,9 +967,8 @@ struct UpdateUint64MetaDataToMaxValue { static void translateSLMInit(CallInst &CI) { auto F = CI.getFunction(); auto *ArgV = CI.getArgOperand(0); - if (!isa(ArgV)) - llvm::report_fatal_error(llvm::Twine(__FILE__ " ") + - "integral constant is expected for slm size"); + llvm::esimd::assert_and_diag(isa(ArgV), __FILE__, + " integral constant is expected for slm size"); uint64_t NewVal = cast(ArgV)->getZExtValue(); assert(NewVal != 0 && "zero slm bytes being requested"); @@ -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(ArgV)) - llvm::report_fatal_error( - llvm::Twine(__FILE__ " ") + - "integral constant is expected for named barrier count"); + llvm::esimd::assert_and_diag( + isa(ArgV), __FILE__, + " integral constant is expected for named barrier count"); auto NewVal = cast(ArgV)->getZExtValue(); assert(NewVal != 0 && "zero named barrier count being requested"); @@ -1000,20 +996,18 @@ static void translateNbarrierInit(CallInst &CI) { static void translatePackMask(CallInst &CI) { using Demangler = id::ManglingParser; 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(AST); llvm::LLVMContext &Context = CI.getContext(); Type *TTy = nullptr; @@ -1042,19 +1036,17 @@ static void translatePackMask(CallInst &CI) { static void translateUnPackMask(CallInst &CI) { using Demangler = id::ManglingParser; 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(AST); llvm::LLVMContext &Context = CI.getContext(); Type *TTy = nullptr; @@ -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: @@ -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); } @@ -1437,19 +1431,17 @@ static Function *createTestESIMDDeclaration(const ESIMDIntrinDesc &Desc, static void translateESIMDIntrinsicCall(CallInst &CI) { using Demangler = id::ManglingParser; 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(AST); id::StringView BaseNameV = FE->getName()->getBaseName();