Debian Codegraph @

llvm-project/llvm/projects/llvm-spirv/lib/SPIRV/LLVMSPIRVOpts.cpp

raw
1 //===- LLVMSPIRVOpts.cpp - Defines LLVM/SPIR-V options ----------*- C++ -*-===//
2 //
3 // The LLVM/SPIR-V Translator
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 // Copyright (c) 2021 Intel Corporation. All rights reserved.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal with the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // Redistributions of source code must retain the above copyright notice,
18 // this list of conditions and the following disclaimers.
19 // Redistributions in binary form must reproduce the above copyright notice,
20 // this list of conditions and the following disclaimers in the documentation
21 // and/or other materials provided with the distribution.
22 // Neither the names of Advanced Micro Devices, Inc., nor the names of its
23 // contributors may be used to endorse or promote products derived from this
24 // Software without specific prior written permission.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
31 // THE SOFTWARE.
32 //
33 //===----------------------------------------------------------------------===//
34 /// \file
35 ///
36 /// This file provides definitions for LLVM/SPIR-V Translator's CLI
37 /// functionality.
38 ///
39 //===----------------------------------------------------------------------===//
40
41 #include "LLVMSPIRVOpts.h"
42
43 #include <llvm/ADT/SmallVector.h>
44 #include <llvm/ADT/StringRef.h>
45 #include <llvm/IR/IntrinsicInst.h>
46 #include <optional>
47
48 using namespace llvm;
49 using namespace SPIRV;
50
51 bool TranslatorOpts::isUnknownIntrinsicAllowed(IntrinsicInst *II) const
52 noexcept {
53 if (!SPIRVAllowUnknownIntrinsics.has_value())
54 return false;
55 const auto &IntrinsicPrefixList = SPIRVAllowUnknownIntrinsics.value();
56 StringRef IntrinsicName = II->getCalledOperand()->getName();
57 for (const auto &Prefix : IntrinsicPrefixList) {
58 if (IntrinsicName.startswith(Prefix)) // Also true if `Prefix` is empty
59 return true;
60 }
61 return false;
62 }
63
64 bool TranslatorOpts::isSPIRVAllowUnknownIntrinsicsEnabled() const noexcept {
65 return SPIRVAllowUnknownIntrinsics.has_value();
66 }
67
68 void TranslatorOpts::setSPIRVAllowUnknownIntrinsics(
69 TranslatorOpts::ArgList IntrinsicPrefixList) noexcept {
70 SPIRVAllowUnknownIntrinsics = IntrinsicPrefixList;
71 }
72
73 bool TranslatorOpts::validateFnVarOpts() const {
74 if (getFnVarCategory() == std::nullopt &&
75 (getFnVarFamily() != std::nullopt || getFnVarArch() != std::nullopt)) {
76 errs() << "FnVar: Device category must be specified if the family or "
77 "architecture are specified.";
78 return false;
79 }
80
81 if (getFnVarFamily() == std::nullopt && getFnVarArch() != std::nullopt) {
82 errs() << "FnVar: Device family must be specified if the architecture is "
83 "specified.";
84 return false;
85 }
86
87 if (getFnVarTarget() == std::nullopt && !getFnVarFeatures().empty()) {
88 errs() << "Device target must be specified if the features are specified.";
89 return false;
90 }
91
92 return true;
93 }