alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 1 | // Copyright 2018 The Clspv Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
alan-baker | 0d095d6 | 2020-03-12 14:59:47 -0400 | [diff] [blame] | 15 | #include "clang/Basic/FileManager.h" |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 16 | #include "clang/Basic/TargetInfo.h" |
| 17 | #include "clang/CodeGen/CodeGenAction.h" |
| 18 | #include "clang/Frontend/CompilerInstance.h" |
| 19 | #include "clang/Frontend/FrontendPluginRegistry.h" |
| 20 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
| 21 | #include "clang/Lex/PreprocessorOptions.h" |
| 22 | #include "llvm/IR/LLVMContext.h" |
| 23 | #include "llvm/IR/LegacyPassManager.h" |
| 24 | #include "llvm/IR/Module.h" |
| 25 | #include "llvm/IR/Verifier.h" |
alan-baker | 0e64a59 | 2019-11-18 13:36:25 -0500 | [diff] [blame] | 26 | #include "llvm/InitializePasses.h" |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 27 | #include "llvm/LinkAllPasses.h" |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 28 | #include "llvm/Support/Allocator.h" |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 29 | #include "llvm/Support/CommandLine.h" |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 30 | #include "llvm/Support/ErrorOr.h" |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 31 | #include "llvm/Support/MathExtras.h" |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 32 | #include "llvm/Support/StringSaver.h" |
Diego Novillo | 8950085 | 2019-04-15 08:45:10 -0400 | [diff] [blame] | 33 | #include "llvm/Support/ToolOutputFile.h" |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
| 35 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
| 36 | |
Kévin Petit | 38c5248 | 2019-05-07 20:28:00 +0800 | [diff] [blame] | 37 | #include "clspv/AddressSpace.h" |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 38 | #include "clspv/DescriptorMap.h" |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 39 | #include "clspv/Option.h" |
| 40 | #include "clspv/Passes.h" |
| 41 | #include "clspv/opencl_builtins_header.h" |
| 42 | |
| 43 | #include "FrontendPlugin.h" |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 44 | #include "Passes.h" |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 45 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 46 | #include <cassert> |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 47 | #include <numeric> |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 48 | #include <sstream> |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 49 | #include <string> |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 50 | |
| 51 | using namespace clang; |
| 52 | |
| 53 | namespace { |
| 54 | // This registration must be located in the same file as the execution of the |
| 55 | // action. |
| 56 | static FrontendPluginRegistry::Add<clspv::ExtraValidationASTAction> |
| 57 | X("extra-validation", |
| 58 | "Perform extra validation on OpenCL C when targeting Vulkan"); |
| 59 | |
| 60 | static llvm::cl::opt<bool> cl_single_precision_constants( |
| 61 | "cl-single-precision-constant", llvm::cl::init(false), |
| 62 | llvm::cl::desc("Treat double precision floating-point constant as single " |
| 63 | "precision constant.")); |
| 64 | |
| 65 | static llvm::cl::opt<bool> cl_denorms_are_zero( |
| 66 | "cl-denorms-are-zero", llvm::cl::init(false), |
| 67 | llvm::cl::desc("If specified, denormalized floating point numbers may be " |
| 68 | "flushed to zero.")); |
| 69 | |
| 70 | static llvm::cl::opt<bool> cl_fp32_correctly_rounded_divide_sqrt( |
| 71 | "cl-fp32-correctly-rounded-divide-sqrt", llvm::cl::init(false), |
| 72 | llvm::cl::desc("Single precision floating-point divide (x/y and 1/x) and " |
| 73 | "sqrt used are correctly rounded.")); |
| 74 | |
| 75 | static llvm::cl::opt<bool> |
| 76 | cl_opt_disable("cl-opt-disable", llvm::cl::init(false), |
| 77 | llvm::cl::desc("This option disables all optimizations. The " |
| 78 | "default is optimizations are enabled.")); |
| 79 | |
| 80 | static llvm::cl::opt<bool> cl_mad_enable( |
| 81 | "cl-mad-enable", llvm::cl::init(false), |
| 82 | llvm::cl::desc("Allow a * b + c to be replaced by a mad. The mad computes " |
| 83 | "a * b + c with reduced accuracy.")); |
| 84 | |
| 85 | static llvm::cl::opt<bool> cl_no_signed_zeros( |
| 86 | "cl-no-signed-zeros", llvm::cl::init(false), |
| 87 | llvm::cl::desc("Allow optimizations for floating-point arithmetic that " |
| 88 | "ignore the signedness of zero.")); |
| 89 | |
| 90 | static llvm::cl::opt<bool> cl_unsafe_math_optimizations( |
| 91 | "cl-unsafe-math-optimizations", llvm::cl::init(false), |
| 92 | llvm::cl::desc("Allow optimizations for floating-point arithmetic that (a) " |
| 93 | "assume that arguments and results are valid, (b) may " |
| 94 | "violate IEEE 754 standard and (c) may violate the OpenCL " |
| 95 | "numerical compliance requirements. This option includes " |
| 96 | "the -cl-no-signed-zeros and -cl-mad-enable options.")); |
| 97 | |
| 98 | static llvm::cl::opt<bool> cl_finite_math_only( |
| 99 | "cl-finite-math-only", llvm::cl::init(false), |
| 100 | llvm::cl::desc("Allow optimizations for floating-point arithmetic that " |
| 101 | "assume that arguments and results are not NaNs or INFs.")); |
| 102 | |
| 103 | static llvm::cl::opt<bool> cl_fast_relaxed_math( |
| 104 | "cl-fast-relaxed-math", llvm::cl::init(false), |
| 105 | llvm::cl::desc("This option causes the preprocessor macro " |
| 106 | "__FAST_RELAXED_MATH__ to be defined. Sets the optimization " |
| 107 | "options -cl-finite-math-only and " |
| 108 | "-cl-unsafe-math-optimizations.")); |
| 109 | |
| 110 | static llvm::cl::list<std::string> |
| 111 | Includes(llvm::cl::Prefix, "I", |
| 112 | llvm::cl::desc("Add a directory to the list of directories " |
| 113 | "to be searched for header files."), |
| 114 | llvm::cl::ZeroOrMore, llvm::cl::value_desc("include path")); |
| 115 | |
| 116 | static llvm::cl::list<std::string> |
| 117 | Defines(llvm::cl::Prefix, "D", |
| 118 | llvm::cl::desc("Define a #define directive."), llvm::cl::ZeroOrMore, |
| 119 | llvm::cl::value_desc("define")); |
| 120 | |
| 121 | static llvm::cl::opt<std::string> |
| 122 | InputFilename(llvm::cl::Positional, llvm::cl::desc("<input .cl file>"), |
| 123 | llvm::cl::init("-")); |
| 124 | |
Kévin Petit | ddad8f4 | 2019-09-30 15:12:08 +0100 | [diff] [blame] | 125 | static llvm::cl::opt<clang::Language> InputLanguage( |
| 126 | "x", llvm::cl::desc("Select input type"), |
| 127 | llvm::cl::init(clang::Language::OpenCL), |
| 128 | llvm::cl::values(clEnumValN(clang::Language::OpenCL, "cl", "OpenCL source"), |
| 129 | clEnumValN(clang::Language::LLVM_IR, "ir", "LLVM IR"))); |
| 130 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 131 | static llvm::cl::opt<std::string> |
| 132 | OutputFilename("o", llvm::cl::desc("Override output filename"), |
| 133 | llvm::cl::value_desc("filename")); |
| 134 | |
| 135 | static llvm::cl::opt<std::string> |
| 136 | DescriptorMapFilename("descriptormap", |
| 137 | llvm::cl::desc("Output file for descriptor map"), |
| 138 | llvm::cl::value_desc("filename")); |
| 139 | |
| 140 | static llvm::cl::opt<char> |
| 141 | OptimizationLevel(llvm::cl::Prefix, "O", llvm::cl::init('2'), |
| 142 | llvm::cl::desc("Optimization level to use"), |
| 143 | llvm::cl::value_desc("level")); |
| 144 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 145 | static llvm::cl::opt<std::string> OutputFormat( |
| 146 | "mfmt", llvm::cl::init(""), |
| 147 | llvm::cl::desc( |
| 148 | "Specify special output format. 'c' is as a C initializer list"), |
| 149 | llvm::cl::value_desc("format")); |
| 150 | |
| 151 | static llvm::cl::opt<std::string> |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 152 | SamplerMap("samplermap", llvm::cl::desc("DEPRECATED - Literal sampler map"), |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 153 | llvm::cl::value_desc("filename")); |
| 154 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 155 | static llvm::cl::opt<bool> verify("verify", llvm::cl::init(false), |
| 156 | llvm::cl::desc("Verify diagnostic outputs")); |
| 157 | |
Kévin Petit | 6b07cbe | 2019-04-02 21:52:16 +0100 | [diff] [blame] | 158 | static llvm::cl::opt<bool> |
| 159 | IgnoreWarnings("w", llvm::cl::init(false), |
| 160 | llvm::cl::desc("Disable all warnings")); |
| 161 | |
| 162 | static llvm::cl::opt<bool> |
| 163 | WarningsAsErrors("Werror", llvm::cl::init(false), |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 164 | llvm::cl::desc("Turn warnings into errors")); |
Kévin Petit | 6b07cbe | 2019-04-02 21:52:16 +0100 | [diff] [blame] | 165 | |
Diego Novillo | 8950085 | 2019-04-15 08:45:10 -0400 | [diff] [blame] | 166 | static llvm::cl::opt<std::string> IROutputFile( |
| 167 | "emit-ir", |
| 168 | llvm::cl::desc( |
| 169 | "Emit LLVM IR to the given file after parsing and stop compilation."), |
| 170 | llvm::cl::value_desc("filename")); |
| 171 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 172 | // Populates |SamplerMapEntries| with data from the input sampler map. Returns 0 |
| 173 | // if successful. |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 174 | int ParseSamplerMap(const std::string &sampler_map, |
| 175 | llvm::SmallVectorImpl<std::pair<unsigned, std::string>> |
| 176 | *SamplerMapEntries) { |
| 177 | std::unique_ptr<llvm::MemoryBuffer> samplerMapBuffer(nullptr); |
| 178 | if (!sampler_map.empty()) { |
| 179 | // Parse the sampler map from the provided string. |
| 180 | samplerMapBuffer = llvm::MemoryBuffer::getMemBuffer(sampler_map); |
| 181 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 182 | clspv::Option::SetUseSamplerMap(true); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 183 | if (!SamplerMap.empty()) { |
| 184 | llvm::outs() << "Warning: -samplermap is ignored when the sampler map is " |
| 185 | "provided through a string.\n"; |
| 186 | } |
| 187 | } else if (!SamplerMap.empty()) { |
| 188 | // Parse the sampler map from the option provided file. |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 189 | auto errorOrSamplerMapFile = |
| 190 | llvm::MemoryBuffer::getFile(SamplerMap.getValue()); |
| 191 | |
| 192 | // If there was an error in getting the sampler map file. |
| 193 | if (!errorOrSamplerMapFile) { |
| 194 | llvm::errs() << "Error: " << errorOrSamplerMapFile.getError().message() |
| 195 | << " '" << SamplerMap.getValue() << "'\n"; |
| 196 | return -1; |
| 197 | } |
| 198 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 199 | clspv::Option::SetUseSamplerMap(true); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 200 | samplerMapBuffer = std::move(errorOrSamplerMapFile.get()); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 201 | if (0 == samplerMapBuffer->getBufferSize()) { |
| 202 | llvm::errs() << "Error: Sampler map was an empty file!\n"; |
| 203 | return -1; |
| 204 | } |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 205 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 206 | |
alan-baker | 09cb980 | 2019-12-10 13:16:27 -0500 | [diff] [blame] | 207 | if (clspv::Option::UseSamplerMap()) { |
| 208 | llvm::outs() |
| 209 | << "Warning: use of the sampler map is deprecated and unnecessary\n"; |
| 210 | } |
| 211 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 212 | // No sampler map to parse. |
| 213 | if (!samplerMapBuffer || 0 == samplerMapBuffer->getBufferSize()) |
| 214 | return 0; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 215 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 216 | llvm::SmallVector<llvm::StringRef, 3> samplerStrings; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 217 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 218 | // We need to keep track of the beginning of the current entry. |
| 219 | const char *b = samplerMapBuffer->getBufferStart(); |
| 220 | for (const char *i = b, *e = samplerMapBuffer->getBufferEnd();; i++) { |
| 221 | // If we have a separator between declarations. |
| 222 | if ((*i == '|') || (*i == ',') || (i == e)) { |
| 223 | if (i == b) { |
| 224 | llvm::errs() << "Error: Sampler map contained an empty entry!\n"; |
| 225 | return -1; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 226 | } |
| 227 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 228 | samplerStrings.push_back(llvm::StringRef(b, i - b).trim()); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 229 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 230 | // And set b the next character after i. |
| 231 | b = i + 1; |
| 232 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 233 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 234 | // If we have a separator between declarations within a single sampler. |
| 235 | if ((*i == ',') || (i == e)) { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 236 | |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 237 | clspv::version0::SamplerNormalizedCoords NormalizedCoord = |
| 238 | clspv::version0::CLK_NORMALIZED_COORDS_NOT_SET; |
| 239 | clspv::version0::SamplerAddressingMode AddressingMode = |
| 240 | clspv::version0::CLK_ADDRESS_NOT_SET; |
| 241 | clspv::version0::SamplerFilterMode FilterMode = |
| 242 | clspv::version0::CLK_FILTER_NOT_SET; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 243 | |
| 244 | for (auto str : samplerStrings) { |
| 245 | if ("CLK_NORMALIZED_COORDS_FALSE" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 246 | if (clspv::version0::CLK_NORMALIZED_COORDS_NOT_SET != |
| 247 | NormalizedCoord) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 248 | llvm::errs() << "Error: Sampler map normalized coordinates was " |
| 249 | "previously set!\n"; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 250 | return -1; |
| 251 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 252 | NormalizedCoord = clspv::version0::CLK_NORMALIZED_COORDS_FALSE; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 253 | } else if ("CLK_NORMALIZED_COORDS_TRUE" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 254 | if (clspv::version0::CLK_NORMALIZED_COORDS_NOT_SET != |
| 255 | NormalizedCoord) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 256 | llvm::errs() << "Error: Sampler map normalized coordinates was " |
| 257 | "previously set!\n"; |
| 258 | return -1; |
| 259 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 260 | NormalizedCoord = clspv::version0::CLK_NORMALIZED_COORDS_TRUE; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 261 | } else if ("CLK_ADDRESS_NONE" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 262 | if (clspv::version0::CLK_ADDRESS_NOT_SET != AddressingMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 263 | llvm::errs() |
| 264 | << "Error: Sampler map addressing mode was previously set!\n"; |
| 265 | return -1; |
| 266 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 267 | AddressingMode = clspv::version0::CLK_ADDRESS_NONE; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 268 | } else if ("CLK_ADDRESS_CLAMP_TO_EDGE" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 269 | if (clspv::version0::CLK_ADDRESS_NOT_SET != AddressingMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 270 | llvm::errs() |
| 271 | << "Error: Sampler map addressing mode was previously set!\n"; |
| 272 | return -1; |
| 273 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 274 | AddressingMode = clspv::version0::CLK_ADDRESS_CLAMP_TO_EDGE; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 275 | } else if ("CLK_ADDRESS_CLAMP" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 276 | if (clspv::version0::CLK_ADDRESS_NOT_SET != AddressingMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 277 | llvm::errs() |
| 278 | << "Error: Sampler map addressing mode was previously set!\n"; |
| 279 | return -1; |
| 280 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 281 | AddressingMode = clspv::version0::CLK_ADDRESS_CLAMP; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 282 | } else if ("CLK_ADDRESS_MIRRORED_REPEAT" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 283 | if (clspv::version0::CLK_ADDRESS_NOT_SET != AddressingMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 284 | llvm::errs() |
| 285 | << "Error: Sampler map addressing mode was previously set!\n"; |
| 286 | return -1; |
| 287 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 288 | AddressingMode = clspv::version0::CLK_ADDRESS_MIRRORED_REPEAT; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 289 | } else if ("CLK_ADDRESS_REPEAT" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 290 | if (clspv::version0::CLK_ADDRESS_NOT_SET != AddressingMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 291 | llvm::errs() |
| 292 | << "Error: Sampler map addressing mode was previously set!\n"; |
| 293 | return -1; |
| 294 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 295 | AddressingMode = clspv::version0::CLK_ADDRESS_REPEAT; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 296 | } else if ("CLK_FILTER_NEAREST" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 297 | if (clspv::version0::CLK_FILTER_NOT_SET != FilterMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 298 | llvm::errs() |
| 299 | << "Error: Sampler map filtering mode was previously set!\n"; |
| 300 | return -1; |
| 301 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 302 | FilterMode = clspv::version0::CLK_FILTER_NEAREST; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 303 | } else if ("CLK_FILTER_LINEAR" == str) { |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 304 | if (clspv::version0::CLK_FILTER_NOT_SET != FilterMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 305 | llvm::errs() |
| 306 | << "Error: Sampler map filtering mode was previously set!\n"; |
| 307 | return -1; |
| 308 | } |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 309 | FilterMode = clspv::version0::CLK_FILTER_LINEAR; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 310 | } else { |
| 311 | llvm::errs() << "Error: Unknown sampler string '" << str |
| 312 | << "' found!\n"; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 313 | return -1; |
| 314 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 315 | } |
| 316 | |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 317 | if (clspv::version0::CLK_NORMALIZED_COORDS_NOT_SET == NormalizedCoord) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 318 | llvm::errs() << "Error: Sampler map entry did not contain normalized " |
| 319 | "coordinates entry!\n"; |
| 320 | return -1; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 321 | } |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 322 | |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 323 | if (clspv::version0::CLK_ADDRESS_NOT_SET == AddressingMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 324 | llvm::errs() << "Error: Sampler map entry did not contain addressing " |
| 325 | "mode entry!\n"; |
| 326 | return -1; |
| 327 | } |
| 328 | |
James Price | c05f605 | 2020-01-14 13:37:20 -0500 | [diff] [blame] | 329 | if (clspv::version0::CLK_FILTER_NOT_SET == FilterMode) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 330 | llvm::errs() |
| 331 | << "Error: Sampler map entry did not contain filer mode entry!\n"; |
| 332 | return -1; |
| 333 | } |
| 334 | |
| 335 | // Generate an equivalent expression in string form. Sort the |
| 336 | // strings to get a canonical ordering. |
| 337 | std::sort(samplerStrings.begin(), samplerStrings.end(), |
| 338 | std::less<StringRef>()); |
| 339 | const auto samplerExpr = std::accumulate( |
| 340 | samplerStrings.begin(), samplerStrings.end(), std::string(), |
alan-baker | 21574d3 | 2020-01-29 16:00:31 -0500 | [diff] [blame] | 341 | [](llvm::StringRef left, llvm::StringRef right) { |
| 342 | return left.str() + std::string(left.empty() ? "" : "|") + |
| 343 | right.str(); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 344 | }); |
| 345 | |
| 346 | // SamplerMapEntries->push_back(std::make_pair( |
| 347 | // NormalizedCoord | AddressingMode | FilterMode, samplerExpr)); |
| 348 | SamplerMapEntries->emplace_back( |
| 349 | NormalizedCoord | AddressingMode | FilterMode, samplerExpr); |
| 350 | |
| 351 | // And reset the sampler strings for the next sampler in the map. |
| 352 | samplerStrings.clear(); |
| 353 | } |
| 354 | |
| 355 | // And lastly, if we are at the end of the file |
| 356 | if (i == e) { |
| 357 | break; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | // Sets |instance|'s options for compiling. Returns 0 if successful. |
| 365 | int SetCompilerInstanceOptions(CompilerInstance &instance, |
| 366 | const llvm::StringRef &overiddenInputFilename, |
| 367 | const clang::FrontendInputFile &kernelFile, |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 368 | const std::string &program, |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 369 | llvm::raw_string_ostream *diagnosticsStream) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 370 | std::unique_ptr<llvm::MemoryBuffer> memory_buffer(nullptr); |
| 371 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> errorOrInputFile(nullptr); |
| 372 | if (program.empty()) { |
| 373 | auto errorOrInputFile = |
| 374 | llvm::MemoryBuffer::getFileOrSTDIN(InputFilename.getValue()); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 375 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 376 | // If there was an error in getting the input file. |
| 377 | if (!errorOrInputFile) { |
| 378 | llvm::errs() << "Error: " << errorOrInputFile.getError().message() << " '" |
| 379 | << InputFilename.getValue() << "'\n"; |
| 380 | return -1; |
| 381 | } |
| 382 | memory_buffer.reset(errorOrInputFile.get().release()); |
| 383 | } else { |
| 384 | memory_buffer = llvm::MemoryBuffer::getMemBuffer(program.c_str(), |
| 385 | overiddenInputFilename); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 386 | } |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 387 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 388 | if (verify) { |
| 389 | instance.getDiagnosticOpts().VerifyDiagnostics = true; |
alan-baker | bccf62c | 2019-03-29 10:32:41 -0400 | [diff] [blame] | 390 | instance.getDiagnosticOpts().VerifyPrefixes.push_back("expected"); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 391 | } |
| 392 | |
Kévin Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 393 | clang::LangStandard::Kind standard; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 394 | switch (clspv::Option::Language()) { |
| 395 | case clspv::Option::SourceLanguage::OpenCL_C_10: |
| 396 | standard = clang::LangStandard::lang_opencl10; |
| 397 | break; |
| 398 | case clspv::Option::SourceLanguage::OpenCL_C_11: |
| 399 | standard = clang::LangStandard::lang_opencl11; |
| 400 | break; |
| 401 | case clspv::Option::SourceLanguage::OpenCL_C_12: |
Kévin Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 402 | standard = clang::LangStandard::lang_opencl12; |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 403 | break; |
| 404 | case clspv::Option::SourceLanguage::OpenCL_C_20: |
| 405 | standard = clang::LangStandard::lang_opencl20; |
| 406 | break; |
| 407 | case clspv::Option::SourceLanguage::OpenCL_CPP: |
| 408 | standard = clang::LangStandard::lang_openclcpp; |
| 409 | break; |
| 410 | default: |
| 411 | llvm_unreachable("Unknown source language"); |
Kévin Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 412 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 413 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 414 | instance.getLangOpts().C99 = true; |
| 415 | instance.getLangOpts().RTTI = false; |
| 416 | instance.getLangOpts().RTTIData = false; |
| 417 | instance.getLangOpts().MathErrno = false; |
| 418 | instance.getLangOpts().Optimize = false; |
| 419 | instance.getLangOpts().NoBuiltin = true; |
| 420 | instance.getLangOpts().ModulesSearchAll = false; |
| 421 | instance.getLangOpts().SinglePrecisionConstants = true; |
| 422 | instance.getCodeGenOpts().StackRealignment = true; |
| 423 | instance.getCodeGenOpts().SimplifyLibCalls = false; |
| 424 | instance.getCodeGenOpts().EmitOpenCLArgMetadata = false; |
| 425 | instance.getCodeGenOpts().DisableO0ImplyOptNone = true; |
Kévin Petit | 6b07cbe | 2019-04-02 21:52:16 +0100 | [diff] [blame] | 426 | instance.getDiagnosticOpts().IgnoreWarnings = IgnoreWarnings; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 427 | |
| 428 | instance.getLangOpts().SinglePrecisionConstants = |
| 429 | cl_single_precision_constants; |
| 430 | // cl_denorms_are_zero ignored for now! |
| 431 | // cl_fp32_correctly_rounded_divide_sqrt ignored for now! |
| 432 | instance.getCodeGenOpts().LessPreciseFPMAD = |
| 433 | cl_mad_enable || cl_unsafe_math_optimizations; |
| 434 | // cl_no_signed_zeros ignored for now! |
| 435 | instance.getCodeGenOpts().UnsafeFPMath = |
| 436 | cl_unsafe_math_optimizations || cl_fast_relaxed_math; |
| 437 | instance.getLangOpts().FiniteMathOnly = |
| 438 | cl_finite_math_only || cl_fast_relaxed_math; |
| 439 | instance.getLangOpts().FastRelaxedMath = cl_fast_relaxed_math; |
| 440 | |
| 441 | // Preprocessor options |
Kévin Petit | a624c0c | 2019-05-07 20:27:43 +0800 | [diff] [blame] | 442 | if (!clspv::Option::ImageSupport()) { |
| 443 | instance.getPreprocessorOpts().addMacroUndef("__IMAGE_SUPPORT__"); |
| 444 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 445 | if (cl_fast_relaxed_math) { |
| 446 | instance.getPreprocessorOpts().addMacroDef("__FAST_RELAXED_MATH__"); |
| 447 | } |
| 448 | |
| 449 | for (auto define : Defines) { |
| 450 | instance.getPreprocessorOpts().addMacroDef(define); |
| 451 | } |
| 452 | |
| 453 | // Header search options |
| 454 | for (auto include : Includes) { |
| 455 | instance.getHeaderSearchOpts().AddPath(include, clang::frontend::After, |
| 456 | false, false); |
| 457 | } |
| 458 | |
| 459 | // We always compile on opt 0 so we preserve as much debug information about |
| 460 | // the source as possible. We'll run optimization later, once we've had a |
| 461 | // chance to view the unoptimal code first |
| 462 | instance.getCodeGenOpts().OptimizationLevel = 0; |
| 463 | |
| 464 | // Debug information is disabled temporarily to call instruction. |
| 465 | #if 0 |
| 466 | instance.getCodeGenOpts().setDebugInfo(clang::codegenoptions::FullDebugInfo); |
| 467 | #endif |
| 468 | |
| 469 | // We use the 32-bit pointer-width SPIR triple |
| 470 | llvm::Triple triple("spir-unknown-unknown"); |
| 471 | |
| 472 | instance.getInvocation().setLangDefaults( |
alan-baker | d354f1a | 2019-08-06 15:41:55 -0400 | [diff] [blame] | 473 | instance.getLangOpts(), clang::InputKind(clang::Language::OpenCL), triple, |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 474 | instance.getPreprocessorOpts(), standard); |
| 475 | |
| 476 | // Override the C99 inline semantics to accommodate for more OpenCL C |
| 477 | // programs in the wild. |
| 478 | instance.getLangOpts().GNUInline = true; |
Kévin Petit | 6b07cbe | 2019-04-02 21:52:16 +0100 | [diff] [blame] | 479 | |
| 480 | // Set up diagnostics |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 481 | instance.createDiagnostics( |
| 482 | new clang::TextDiagnosticPrinter(*diagnosticsStream, |
| 483 | &instance.getDiagnosticOpts()), |
| 484 | true); |
Kévin Petit | 6b07cbe | 2019-04-02 21:52:16 +0100 | [diff] [blame] | 485 | instance.getDiagnostics().setWarningsAsErrors(WarningsAsErrors); |
| 486 | instance.getDiagnostics().setEnableAllWarnings(true); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 487 | |
| 488 | instance.getTargetOpts().Triple = triple.str(); |
| 489 | |
alan-baker | 21574d3 | 2020-01-29 16:00:31 -0500 | [diff] [blame] | 490 | instance.getCodeGenOpts().MainFileName = overiddenInputFilename.str(); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 491 | instance.getCodeGenOpts().PreserveVec3Type = true; |
| 492 | // Disable generation of lifetime intrinsic. |
| 493 | instance.getCodeGenOpts().DisableLifetimeMarkers = true; |
| 494 | instance.getFrontendOpts().Inputs.push_back(kernelFile); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 495 | instance.getPreprocessorOpts().addRemappedFile(overiddenInputFilename, |
| 496 | memory_buffer.release()); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 497 | |
| 498 | struct OpenCLBuiltinMemoryBuffer final : public llvm::MemoryBuffer { |
| 499 | OpenCLBuiltinMemoryBuffer(const void *data, uint64_t data_length) { |
| 500 | const char *dataCasted = reinterpret_cast<const char *>(data); |
| 501 | init(dataCasted, dataCasted + data_length, true); |
| 502 | } |
| 503 | |
| 504 | virtual llvm::MemoryBuffer::BufferKind getBufferKind() const override { |
| 505 | return llvm::MemoryBuffer::MemoryBuffer_Malloc; |
| 506 | } |
| 507 | |
| 508 | virtual ~OpenCLBuiltinMemoryBuffer() override {} |
| 509 | }; |
| 510 | |
| 511 | std::unique_ptr<llvm::MemoryBuffer> openCLBuiltinMemoryBuffer( |
| 512 | new OpenCLBuiltinMemoryBuffer(opencl_builtins_header_data, |
| 513 | opencl_builtins_header_size - 1)); |
| 514 | |
| 515 | instance.getPreprocessorOpts().Includes.push_back("openclc.h"); |
| 516 | |
alan-baker | f3bce4a | 2019-06-28 16:01:15 -0400 | [diff] [blame] | 517 | std::unique_ptr<llvm::MemoryBuffer> openCLBaseBuiltinMemoryBuffer( |
| 518 | new OpenCLBuiltinMemoryBuffer(opencl_base_builtins_header_data, |
| 519 | opencl_base_builtins_header_size - 1)); |
| 520 | |
| 521 | instance.getPreprocessorOpts().Includes.push_back("opencl-c-base.h"); |
| 522 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 523 | // Add the VULKAN macro. |
| 524 | instance.getPreprocessorOpts().addMacroDef("VULKAN=100"); |
| 525 | |
| 526 | // Add the __OPENCL_VERSION__ macro. |
| 527 | instance.getPreprocessorOpts().addMacroDef("__OPENCL_VERSION__=120"); |
| 528 | |
| 529 | instance.setTarget(clang::TargetInfo::CreateTargetInfo( |
| 530 | instance.getDiagnostics(), |
| 531 | std::make_shared<clang::TargetOptions>(instance.getTargetOpts()))); |
| 532 | |
| 533 | instance.createFileManager(); |
| 534 | instance.createSourceManager(instance.getFileManager()); |
| 535 | |
| 536 | #ifdef _MSC_VER |
| 537 | std::string includePrefix("include\\"); |
| 538 | #else |
| 539 | std::string includePrefix("include/"); |
| 540 | #endif |
| 541 | |
| 542 | auto entry = instance.getFileManager().getVirtualFile( |
| 543 | includePrefix + "openclc.h", openCLBuiltinMemoryBuffer->getBufferSize(), |
| 544 | 0); |
| 545 | |
| 546 | instance.getSourceManager().overrideFileContents( |
| 547 | entry, std::move(openCLBuiltinMemoryBuffer)); |
| 548 | |
alan-baker | f3bce4a | 2019-06-28 16:01:15 -0400 | [diff] [blame] | 549 | auto base_entry = instance.getFileManager().getVirtualFile( |
| 550 | includePrefix + "opencl-c-base.h", |
| 551 | openCLBaseBuiltinMemoryBuffer->getBufferSize(), 0); |
| 552 | |
| 553 | instance.getSourceManager().overrideFileContents( |
| 554 | base_entry, std::move(openCLBaseBuiltinMemoryBuffer)); |
| 555 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 556 | return 0; |
| 557 | } |
| 558 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 559 | // Populates |pm| with necessary passes to optimize and legalize the IR. |
| 560 | int PopulatePassManager( |
| 561 | llvm::legacy::PassManager *pm, llvm::raw_svector_ostream *binaryStream, |
| 562 | std::vector<clspv::version0::DescriptorMapEntry> *descriptor_map_entries, |
| 563 | llvm::SmallVectorImpl<std::pair<unsigned, std::string>> |
| 564 | *SamplerMapEntries) { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 565 | llvm::PassManagerBuilder pmBuilder; |
| 566 | |
| 567 | switch (OptimizationLevel) { |
| 568 | case '0': |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 569 | case '1': |
| 570 | case '2': |
| 571 | case '3': |
| 572 | case 's': |
| 573 | case 'z': |
| 574 | break; |
| 575 | default: |
| 576 | llvm::errs() << "Unknown optimization level -O" << OptimizationLevel |
| 577 | << " specified!\n"; |
| 578 | return -1; |
| 579 | } |
| 580 | |
| 581 | switch (OptimizationLevel) { |
| 582 | case '0': |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 583 | pmBuilder.OptLevel = 0; |
| 584 | break; |
| 585 | case '1': |
| 586 | pmBuilder.OptLevel = 1; |
| 587 | break; |
| 588 | case '2': |
| 589 | pmBuilder.OptLevel = 2; |
| 590 | break; |
| 591 | case '3': |
| 592 | pmBuilder.OptLevel = 3; |
| 593 | break; |
| 594 | case 's': |
| 595 | pmBuilder.SizeLevel = 1; |
| 596 | break; |
| 597 | case 'z': |
| 598 | pmBuilder.SizeLevel = 2; |
| 599 | break; |
| 600 | default: |
| 601 | break; |
| 602 | } |
| 603 | |
| 604 | pm->add(clspv::createZeroInitializeAllocasPass()); |
alan-baker | 04f3a95 | 2020-03-24 10:39:53 -0400 | [diff] [blame] | 605 | pm->add(clspv::createAddFunctionAttributesPass()); |
alan-baker | c4579bb | 2020-04-29 14:15:50 -0400 | [diff] [blame] | 606 | pm->add(clspv::createAutoPodArgsPass()); |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 607 | pm->add(clspv::createDeclarePushConstantsPass()); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 608 | pm->add(clspv::createDefineOpenCLWorkItemBuiltinsPass()); |
| 609 | |
| 610 | if (0 < pmBuilder.OptLevel) { |
| 611 | pm->add(clspv::createOpenCLInlinerPass()); |
| 612 | } |
| 613 | |
| 614 | pm->add(clspv::createUndoByvalPass()); |
| 615 | pm->add(clspv::createUndoSRetPass()); |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 616 | if (clspv::Option::ClusterPodKernelArgs()) { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 617 | pm->add(clspv::createClusterPodKernelArgumentsPass()); |
| 618 | } |
| 619 | pm->add(clspv::createReplaceOpenCLBuiltinPass()); |
| 620 | |
| 621 | // We need to run mem2reg and inst combine early because our |
| 622 | // createInlineFuncWithPointerBitCastArgPass pass cannot handle the pattern |
| 623 | // %1 = alloca i32 1 |
| 624 | // store <something> %1 |
| 625 | // %2 = bitcast float* %1 |
| 626 | // %3 = load float %2 |
| 627 | pm->add(llvm::createPromoteMemoryToRegisterPass()); |
| 628 | |
alan-baker | 1b13e8f | 2019-08-08 17:56:51 -0400 | [diff] [blame] | 629 | // Try to deal with pointer bitcasts early. This can prevent problems like |
| 630 | // issue #409 where LLVM is looser about access chain addressing than SPIR-V. |
| 631 | // This needs to happen before instcombine and after replacing OpenCL |
| 632 | // builtins. This run of the pass will not handle all pointer bitcasts that |
| 633 | // could be handled. It should be run again after other optimizations (e.g |
| 634 | // InlineFuncWithPointerBitCastArgPass). |
| 635 | pm->add(clspv::createSimplifyPointerBitcastPass()); |
| 636 | pm->add(clspv::createReplacePointerBitcastPass()); |
| 637 | pm->add(llvm::createDeadCodeEliminationPass()); |
| 638 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 639 | // Hide loads from __constant address space away from instcombine. |
| 640 | // This prevents us from generating select between pointers-to-__constant. |
| 641 | // See https://github.com/google/clspv/issues/71 |
| 642 | pm->add(clspv::createHideConstantLoadsPass()); |
| 643 | |
| 644 | pm->add(llvm::createInstructionCombiningPass()); |
| 645 | |
| 646 | if (clspv::Option::InlineEntryPoints()) { |
| 647 | pm->add(clspv::createInlineEntryPointsPass()); |
| 648 | } else { |
| 649 | pm->add(clspv::createInlineFuncWithPointerBitCastArgPass()); |
| 650 | pm->add(clspv::createInlineFuncWithPointerToFunctionArgPass()); |
| 651 | pm->add(clspv::createInlineFuncWithSingleCallSitePass()); |
| 652 | } |
| 653 | |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 654 | if (clspv::Option::LanguageUsesGenericAddressSpace()) { |
Kévin Petit | 38c5248 | 2019-05-07 20:28:00 +0800 | [diff] [blame] | 655 | pm->add(llvm::createInferAddressSpacesPass(clspv::AddressSpace::Generic)); |
Kévin Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 656 | } |
| 657 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 658 | if (0 == pmBuilder.OptLevel) { |
| 659 | // Mem2Reg pass should be run early because O0 level optimization leaves |
| 660 | // redundant alloca, load and store instructions from function arguments. |
| 661 | // clspv needs to remove them ahead of transformation. |
| 662 | pm->add(llvm::createPromoteMemoryToRegisterPass()); |
| 663 | |
| 664 | // SROA pass is run because it will fold structs/unions that are problematic |
| 665 | // on Vulkan SPIR-V away. |
| 666 | pm->add(llvm::createSROAPass()); |
| 667 | |
| 668 | // InstructionCombining pass folds bitcast and gep instructions which are |
| 669 | // not supported by Vulkan SPIR-V. |
| 670 | pm->add(llvm::createInstructionCombiningPass()); |
| 671 | } |
| 672 | |
| 673 | // Now we add any of the LLVM optimizations we wanted |
| 674 | pmBuilder.populateModulePassManager(*pm); |
| 675 | |
alan-baker | b5e74d6 | 2020-04-07 20:38:05 -0400 | [diff] [blame] | 676 | // No point attempting to handle freeze currently so strip them from the IR. |
| 677 | pm->add(clspv::createStripFreezePass()); |
| 678 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 679 | // Unhide loads from __constant address space. Undoes the action of |
| 680 | // HideConstantLoadsPass. |
| 681 | pm->add(clspv::createUnhideConstantLoadsPass()); |
| 682 | |
alan-baker | 1356838 | 2020-04-02 17:29:27 -0400 | [diff] [blame] | 683 | pm->add(clspv::createUndoInstCombinePass()); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 684 | pm->add(clspv::createFunctionInternalizerPass()); |
| 685 | pm->add(clspv::createReplaceLLVMIntrinsicsPass()); |
| 686 | pm->add(clspv::createUndoBoolPass()); |
| 687 | pm->add(clspv::createUndoTruncatedSwitchConditionPass()); |
| 688 | pm->add(llvm::createStructurizeCFGPass(false)); |
alan-baker | 3fa76d9 | 2018-11-12 14:54:40 -0500 | [diff] [blame] | 689 | // Must be run after structurize cfg. |
alan-baker | 9580aef | 2020-01-07 22:31:48 -0500 | [diff] [blame] | 690 | pm->add(clspv::createFixupStructuredCFGPass()); |
| 691 | // Must be run after structured cfg fixup. |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 692 | pm->add(clspv::createReorderBasicBlocksPass()); |
| 693 | pm->add(clspv::createUndoGetElementPtrConstantExprPass()); |
| 694 | pm->add(clspv::createSplatArgPass()); |
| 695 | pm->add(clspv::createSimplifyPointerBitcastPass()); |
| 696 | pm->add(clspv::createReplacePointerBitcastPass()); |
| 697 | |
| 698 | pm->add(clspv::createUndoTranslateSamplerFoldPass()); |
| 699 | |
| 700 | if (clspv::Option::ModuleConstantsInStorageBuffer()) { |
| 701 | pm->add(clspv::createClusterModuleScopeConstantVars()); |
| 702 | } |
| 703 | |
| 704 | pm->add(clspv::createShareModuleScopeVariablesPass()); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 705 | // Specialize images before assigning descriptors to disambiguate the various |
| 706 | // types. |
| 707 | pm->add(clspv::createSpecializeImageTypesPass()); |
alan-baker | e930801 | 2019-03-15 10:25:13 -0400 | [diff] [blame] | 708 | // This should be run after LLVM and OpenCL intrinsics are replaced. |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 709 | pm->add(clspv::createAllocateDescriptorsPass(*SamplerMapEntries)); |
| 710 | pm->add(llvm::createVerifierPass()); |
| 711 | pm->add(clspv::createDirectResourceAccessPass()); |
| 712 | // Replacing pointer bitcasts can leave some trivial GEPs |
| 713 | // that are easy to remove. Also replace GEPs of GEPS |
| 714 | // left by replacing indirect buffer accesses. |
| 715 | pm->add(clspv::createSimplifyPointerBitcastPass()); |
alan-baker | 4217b32 | 2019-03-06 08:56:12 -0500 | [diff] [blame] | 716 | // Run after DRA to clean up parameters and help reduce the need for variable |
| 717 | // pointers. |
| 718 | pm->add(clspv::createRemoveUnusedArgumentsPass()); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 719 | |
| 720 | pm->add(clspv::createSplatSelectConditionPass()); |
| 721 | pm->add(clspv::createSignedCompareFixupPass()); |
| 722 | // This pass generates insertions that need to be rewritten. |
| 723 | pm->add(clspv::createScalarizePass()); |
| 724 | pm->add(clspv::createRewriteInsertsPass()); |
alan-baker | a71f193 | 2019-04-11 11:04:34 -0400 | [diff] [blame] | 725 | // UBO Transformations |
| 726 | if (clspv::Option::ConstantArgsInUniformBuffer() && |
| 727 | !clspv::Option::InlineEntryPoints()) { |
| 728 | // MultiVersionUBOFunctionsPass will examine non-kernel functions with UBO |
| 729 | // arguments and either multi-version them as necessary or inline them if |
| 730 | // multi-versioning cannot be accomplished. |
| 731 | pm->add(clspv::createMultiVersionUBOFunctionsPass()); |
| 732 | // Cleanup passes. |
| 733 | // Specialization can blindly generate GEP chains that are easily cleaned up |
| 734 | // by SimplifyPointerBitcastPass. |
| 735 | pm->add(clspv::createSimplifyPointerBitcastPass()); |
| 736 | // RemoveUnusedArgumentsPass removes the actual UBO arguments that were |
| 737 | // problematic to begin with now that they have no uses. |
| 738 | pm->add(clspv::createRemoveUnusedArgumentsPass()); |
| 739 | // DCE cleans up callers of the specialized functions. |
| 740 | pm->add(llvm::createDeadCodeEliminationPass()); |
| 741 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 742 | // This pass mucks with types to point where you shouldn't rely on DataLayout |
| 743 | // anymore so leave this right before SPIR-V generation. |
| 744 | pm->add(clspv::createUBOTypeTransformPass()); |
alan-baker | 00e7a58 | 2019-06-07 12:54:21 -0400 | [diff] [blame] | 745 | pm->add(clspv::createSPIRVProducerPass(*binaryStream, descriptor_map_entries, |
| 746 | *SamplerMapEntries, |
| 747 | OutputFormat == "c")); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 748 | |
| 749 | return 0; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 750 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 751 | |
Kévin Petit | d5db2d2 | 2019-04-04 13:55:14 +0100 | [diff] [blame] | 752 | int ParseOptions(const int argc, const char *const argv[]) { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 753 | // We need to change how one of the called passes works by spoofing |
| 754 | // ParseCommandLineOptions with the specific option. |
| 755 | const int llvmArgc = 2; |
| 756 | const char *llvmArgv[llvmArgc] = { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 757 | argv[0], |
| 758 | "-simplifycfg-sink-common=false", |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 759 | }; |
| 760 | |
Kévin Petit | d5db2d2 | 2019-04-04 13:55:14 +0100 | [diff] [blame] | 761 | llvm::cl::ResetAllOptionOccurrences(); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 762 | llvm::cl::ParseCommandLineOptions(llvmArgc, llvmArgv); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 763 | llvm::cl::ParseCommandLineOptions(argc, argv); |
| 764 | |
Kévin Petit | f051571 | 2020-01-07 18:29:20 +0000 | [diff] [blame] | 765 | if (clspv::Option::LanguageUsesGenericAddressSpace() && |
| 766 | !clspv::Option::InlineEntryPoints()) { |
| 767 | llvm::errs() << "cannot compile languages that use the generic address " |
| 768 | "space (e.g. CLC++, CL2.0) without -inline-entry-points\n"; |
Kévin Petit | 0fc8804 | 2019-04-09 23:25:02 +0100 | [diff] [blame] | 769 | return -1; |
| 770 | } |
| 771 | |
Kévin Petit | bbbda97 | 2020-03-03 19:16:31 +0000 | [diff] [blame] | 772 | if (clspv::Option::ScalarBlockLayout()) { |
| 773 | llvm::errs() << "scalar block layout support unimplemented\n"; |
| 774 | return -1; |
| 775 | } |
| 776 | |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 777 | // Push constant option validation. |
| 778 | if (clspv::Option::PodArgsInPushConstants()) { |
| 779 | if (clspv::Option::PodArgsInUniformBuffer()) { |
| 780 | llvm::errs() << "POD arguments can only be in either uniform buffers or " |
| 781 | "push constants\n"; |
| 782 | return -1; |
| 783 | } |
| 784 | |
| 785 | if (!clspv::Option::ClusterPodKernelArgs()) { |
| 786 | llvm::errs() |
| 787 | << "POD arguments must be clustered to be passed as push constants\n"; |
| 788 | return -1; |
| 789 | } |
| 790 | |
| 791 | // Conservatively error if a module scope push constant could be used. |
James Price | 708cf36 | 2020-05-06 19:33:45 -0400 | [diff] [blame] | 792 | if (clspv::Option::GlobalOffsetPushConstant() || |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 793 | clspv::Option::Language() == |
| 794 | clspv::Option::SourceLanguage::OpenCL_C_20 || |
| 795 | clspv::Option::Language() == |
| 796 | clspv::Option::SourceLanguage::OpenCL_CPP) { |
| 797 | llvm::errs() << "POD arguments as push constants are not compatible with " |
| 798 | "module scope push constants\n"; |
| 799 | return -1; |
| 800 | } |
| 801 | } |
| 802 | |
Kévin Petit | d5db2d2 | 2019-04-04 13:55:14 +0100 | [diff] [blame] | 803 | return 0; |
| 804 | } |
Diego Novillo | 8950085 | 2019-04-15 08:45:10 -0400 | [diff] [blame] | 805 | |
| 806 | int GenerateIRFile(llvm::legacy::PassManager *pm, llvm::Module &module, |
| 807 | std::string output) { |
| 808 | std::error_code ec; |
| 809 | std::unique_ptr<llvm::ToolOutputFile> out( |
| 810 | new llvm::ToolOutputFile(output, ec, llvm::sys::fs::F_None)); |
| 811 | if (ec) { |
| 812 | llvm::errs() << output << ": " << ec.message() << '\n'; |
| 813 | return -1; |
| 814 | } |
| 815 | pm->add(llvm::createPrintModulePass(out->os(), "", false)); |
| 816 | pm->run(module); |
| 817 | out->keep(); |
| 818 | return 0; |
| 819 | } |
| 820 | |
Kévin Petit | d5db2d2 | 2019-04-04 13:55:14 +0100 | [diff] [blame] | 821 | } // namespace |
| 822 | |
| 823 | namespace clspv { |
| 824 | int Compile(const int argc, const char *const argv[]) { |
| 825 | |
| 826 | if (auto error = ParseOptions(argc, argv)) |
| 827 | return error; |
| 828 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 829 | llvm::SmallVector<std::pair<unsigned, std::string>, 8> SamplerMapEntries; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 830 | if (auto error = ParseSamplerMap("", &SamplerMapEntries)) |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 831 | return error; |
| 832 | |
| 833 | // if no output file was provided, use a default |
| 834 | llvm::StringRef overiddenInputFilename = InputFilename.getValue(); |
| 835 | |
| 836 | // If we are reading our input file from stdin. |
| 837 | if ("-" == InputFilename) { |
| 838 | // We need to overwrite the file name we use. |
Kévin Petit | ddad8f4 | 2019-09-30 15:12:08 +0100 | [diff] [blame] | 839 | switch (InputLanguage) { |
| 840 | case clang::Language::OpenCL: |
| 841 | overiddenInputFilename = "stdin.cl"; |
| 842 | break; |
| 843 | case clang::Language::LLVM_IR: |
| 844 | overiddenInputFilename = "stdin.ll"; |
| 845 | break; |
alan-baker | 31298a6 | 2019-10-07 13:24:30 -0400 | [diff] [blame] | 846 | default: |
| 847 | // Default to fix compiler warnings/errors. Option parsing will reject a |
| 848 | // bad enum value for the option so there is no need for a message. |
| 849 | return -1; |
Kévin Petit | ddad8f4 | 2019-09-30 15:12:08 +0100 | [diff] [blame] | 850 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | clang::CompilerInstance instance; |
Kévin Petit | ddad8f4 | 2019-09-30 15:12:08 +0100 | [diff] [blame] | 854 | clang::FrontendInputFile kernelFile(overiddenInputFilename, |
| 855 | clang::InputKind(InputLanguage)); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 856 | std::string log; |
| 857 | llvm::raw_string_ostream diagnosticsStream(log); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 858 | if (auto error = SetCompilerInstanceOptions( |
| 859 | instance, overiddenInputFilename, kernelFile, "", &diagnosticsStream)) |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 860 | return error; |
| 861 | |
| 862 | // Parse. |
| 863 | llvm::LLVMContext context; |
| 864 | clang::EmitLLVMOnlyAction action(&context); |
| 865 | |
| 866 | // Prepare the action for processing kernelFile |
| 867 | const bool success = action.BeginSourceFile(instance, kernelFile); |
| 868 | if (!success) { |
| 869 | return -1; |
| 870 | } |
| 871 | |
alan-baker | f3bce4a | 2019-06-28 16:01:15 -0400 | [diff] [blame] | 872 | auto result = action.Execute(); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 873 | action.EndSourceFile(); |
| 874 | |
| 875 | clang::DiagnosticConsumer *const consumer = |
| 876 | instance.getDiagnostics().getClient(); |
| 877 | consumer->finish(); |
| 878 | |
Kévin Petit | 6b07cbe | 2019-04-02 21:52:16 +0100 | [diff] [blame] | 879 | auto num_warnings = consumer->getNumWarnings(); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 880 | auto num_errors = consumer->getNumErrors(); |
Kévin Petit | 6b07cbe | 2019-04-02 21:52:16 +0100 | [diff] [blame] | 881 | if ((num_errors > 0) || (num_warnings > 0)) { |
| 882 | llvm::errs() << log; |
| 883 | } |
alan-baker | f3bce4a | 2019-06-28 16:01:15 -0400 | [diff] [blame] | 884 | if (result || num_errors > 0) { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 885 | return -1; |
| 886 | } |
| 887 | |
Kévin Petit | 6b07cbe | 2019-04-02 21:52:16 +0100 | [diff] [blame] | 888 | // Don't run the passes or produce any output in verify mode. |
| 889 | // Clang doesn't always produce a valid module. |
| 890 | if (verify) { |
| 891 | return 0; |
| 892 | } |
| 893 | |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 894 | llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry(); |
| 895 | llvm::initializeCore(Registry); |
| 896 | llvm::initializeScalarOpts(Registry); |
Diego Novillo | 1fcff72 | 2019-05-07 13:45:53 -0400 | [diff] [blame] | 897 | llvm::initializeClspvPasses(Registry); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 898 | |
| 899 | std::unique_ptr<llvm::Module> module(action.takeModule()); |
| 900 | |
| 901 | // Optimize. |
| 902 | // Create a memory buffer for temporarily writing the result. |
| 903 | SmallVector<char, 10000> binary; |
| 904 | llvm::raw_svector_ostream binaryStream(binary); |
| 905 | std::string descriptor_map; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 906 | llvm::legacy::PassManager pm; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 907 | std::vector<version0::DescriptorMapEntry> descriptor_map_entries; |
Diego Novillo | 8950085 | 2019-04-15 08:45:10 -0400 | [diff] [blame] | 908 | |
| 909 | // If --emit-ir was requested, emit the initial LLVM IR and stop compilation. |
| 910 | if (!IROutputFile.empty()) { |
| 911 | return GenerateIRFile(&pm, *module, IROutputFile); |
| 912 | } |
| 913 | |
| 914 | // Otherwise, populate the pass manager and run the regular passes. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 915 | if (auto error = PopulatePassManager( |
| 916 | &pm, &binaryStream, &descriptor_map_entries, &SamplerMapEntries)) |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 917 | return error; |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 918 | pm.run(*module); |
| 919 | |
| 920 | // Write outputs |
| 921 | |
| 922 | // Write the descriptor map, if requested. |
| 923 | std::error_code error; |
| 924 | if (!DescriptorMapFilename.empty()) { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 925 | llvm::raw_fd_ostream descriptor_map_out_fd( |
| 926 | DescriptorMapFilename, error, llvm::sys::fs::CD_CreateAlways, |
| 927 | llvm::sys::fs::FA_Write, llvm::sys::fs::F_Text); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 928 | if (error) { |
| 929 | llvm::errs() << "Unable to open descriptor map file '" |
| 930 | << DescriptorMapFilename << "': " << error.message() << '\n'; |
| 931 | return -1; |
| 932 | } |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 933 | std::string descriptor_map_string; |
| 934 | std::ostringstream str(descriptor_map_string); |
| 935 | for (const auto &entry : descriptor_map_entries) { |
| 936 | str << entry << "\n"; |
| 937 | } |
| 938 | descriptor_map_out_fd << str.str(); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 939 | descriptor_map_out_fd.close(); |
| 940 | } |
| 941 | |
| 942 | // Write the resulting binary. |
| 943 | // Wait until now to try writing the file so that we only write it on |
| 944 | // successful compilation. |
| 945 | if (OutputFilename.empty()) { |
Kévin Petit | e478690 | 2019-04-02 21:51:47 +0100 | [diff] [blame] | 946 | if (OutputFormat == "c") { |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 947 | OutputFilename = "a.spvinc"; |
| 948 | } else { |
| 949 | OutputFilename = "a.spv"; |
| 950 | } |
| 951 | } |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 952 | llvm::raw_fd_ostream outStream(OutputFilename, error, |
| 953 | llvm::sys::fs::FA_Write); |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 954 | |
| 955 | if (error) { |
| 956 | llvm::errs() << "Unable to open output file '" << OutputFilename |
| 957 | << "': " << error.message() << '\n'; |
| 958 | return -1; |
| 959 | } |
| 960 | outStream << binaryStream.str(); |
| 961 | |
| 962 | return 0; |
| 963 | } |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 964 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 965 | int CompileFromSourceString( |
| 966 | const std::string &program, const std::string &sampler_map, |
| 967 | const std::string &options, std::vector<uint32_t> *output_binary, |
| 968 | std::vector<clspv::version0::DescriptorMapEntry> *descriptor_map_entries) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 969 | |
| 970 | llvm::SmallVector<const char *, 20> argv; |
| 971 | llvm::BumpPtrAllocator A; |
| 972 | llvm::StringSaver Saver(A); |
| 973 | argv.push_back(Saver.save("clspv").data()); |
| 974 | llvm::cl::TokenizeGNUCommandLine(options, Saver, argv); |
| 975 | int argc = static_cast<int>(argv.size()); |
Kévin Petit | d5db2d2 | 2019-04-04 13:55:14 +0100 | [diff] [blame] | 976 | |
| 977 | if (auto error = ParseOptions(argc, &argv[0])) |
| 978 | return error; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 979 | |
| 980 | llvm::SmallVector<std::pair<unsigned, std::string>, 8> SamplerMapEntries; |
| 981 | if (auto error = ParseSamplerMap(sampler_map, &SamplerMapEntries)) |
| 982 | return error; |
| 983 | |
| 984 | InputFilename = "source.cl"; |
| 985 | llvm::StringRef overiddenInputFilename = InputFilename.getValue(); |
| 986 | |
| 987 | clang::CompilerInstance instance; |
alan-baker | d354f1a | 2019-08-06 15:41:55 -0400 | [diff] [blame] | 988 | clang::FrontendInputFile kernelFile( |
| 989 | overiddenInputFilename, clang::InputKind(clang::Language::OpenCL)); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 990 | std::string log; |
| 991 | llvm::raw_string_ostream diagnosticsStream(log); |
| 992 | if (auto error = |
| 993 | SetCompilerInstanceOptions(instance, overiddenInputFilename, |
| 994 | kernelFile, program, &diagnosticsStream)) |
| 995 | return error; |
| 996 | |
| 997 | // Parse. |
| 998 | llvm::LLVMContext context; |
| 999 | clang::EmitLLVMOnlyAction action(&context); |
| 1000 | |
| 1001 | // Prepare the action for processing kernelFile |
| 1002 | const bool success = action.BeginSourceFile(instance, kernelFile); |
| 1003 | if (!success) { |
| 1004 | return -1; |
| 1005 | } |
| 1006 | |
alan-baker | f3bce4a | 2019-06-28 16:01:15 -0400 | [diff] [blame] | 1007 | auto result = action.Execute(); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 1008 | action.EndSourceFile(); |
| 1009 | |
| 1010 | clang::DiagnosticConsumer *const consumer = |
| 1011 | instance.getDiagnostics().getClient(); |
| 1012 | consumer->finish(); |
| 1013 | |
| 1014 | auto num_errors = consumer->getNumErrors(); |
alan-baker | f3bce4a | 2019-06-28 16:01:15 -0400 | [diff] [blame] | 1015 | if (result || num_errors > 0) { |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 1016 | llvm::errs() << log << "\n"; |
| 1017 | return -1; |
| 1018 | } |
| 1019 | |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 1020 | llvm::PassRegistry &Registry = *llvm::PassRegistry::getPassRegistry(); |
| 1021 | llvm::initializeCore(Registry); |
| 1022 | llvm::initializeScalarOpts(Registry); |
Diego Novillo | 1fcff72 | 2019-05-07 13:45:53 -0400 | [diff] [blame] | 1023 | llvm::initializeClspvPasses(Registry); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 1024 | |
| 1025 | std::unique_ptr<llvm::Module> module(action.takeModule()); |
| 1026 | |
| 1027 | // Optimize. |
| 1028 | // Create a memory buffer for temporarily writing the result. |
| 1029 | SmallVector<char, 10000> binary; |
| 1030 | llvm::raw_svector_ostream binaryStream(binary); |
| 1031 | std::string descriptor_map; |
| 1032 | llvm::legacy::PassManager pm; |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1033 | if (auto error = PopulatePassManager( |
| 1034 | &pm, &binaryStream, descriptor_map_entries, &SamplerMapEntries)) |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 1035 | return error; |
| 1036 | pm.run(*module); |
| 1037 | |
| 1038 | // Write outputs |
| 1039 | |
| 1040 | // Write the descriptor map. This is required. |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1041 | assert(descriptor_map_entries && |
| 1042 | "Valid descriptor map container is required."); |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 1043 | if (!DescriptorMapFilename.empty()) { |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 1044 | llvm::errs() << "Warning: -descriptormap is ignored descriptor map " |
| 1045 | "container is provided.\n"; |
alan-baker | f5e5f69 | 2018-11-27 08:33:24 -0500 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | // Write the resulting binary. |
| 1049 | // Wait until now to try writing the file so that we only write it on |
| 1050 | // successful compilation. |
| 1051 | assert(output_binary && "Valid binary container is required."); |
| 1052 | if (!OutputFilename.empty()) { |
| 1053 | llvm::outs() |
| 1054 | << "Warning: -o is ignored when binary container is provided.\n"; |
| 1055 | } |
| 1056 | output_binary->resize(binary.size() / 4); |
| 1057 | memcpy(output_binary->data(), binary.data(), binary.size()); |
| 1058 | |
| 1059 | return 0; |
| 1060 | } |
alan-baker | fec0a47 | 2018-11-08 18:09:40 -0500 | [diff] [blame] | 1061 | } // namespace clspv |