|
| 1 | +/* |
| 2 | + * Copyright (C) 2024 Intel Corporation |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, |
| 11 | + * software distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions |
| 14 | + * and limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-License-Identifier: Apache-2.0 |
| 17 | + */ |
| 18 | + |
| 19 | +#include "gc/ExecutionEngine/Driver/Driver.h" |
| 20 | +#include "gc/ExecutionEngine/GPURuntime/GpuOclRuntime.h" |
| 21 | +#include "gc/Transforms/Passes.h" |
| 22 | +#include "gc/Utils/Error.h" |
| 23 | + |
| 24 | +#include "mlir/Dialect/Arith/IR/Arith.h" |
| 25 | +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
| 26 | +#include "mlir/ExecutionEngine/JitRunner.h" |
| 27 | +#include "mlir/ExecutionEngine/OptUtils.h" |
| 28 | +#include "mlir/IR/MLIRContext.h" |
| 29 | +#include "mlir/Support/FileUtilities.h" |
| 30 | +#include "mlir/Tools/ParseUtilities.h" |
| 31 | +#include "mlir/Transforms/Passes.h" |
| 32 | + |
| 33 | +#include "llvm/Support/CommandLine.h" |
| 34 | +#include "llvm/Support/InitLLVM.h" |
| 35 | +#include "llvm/Support/SourceMgr.h" |
| 36 | + |
| 37 | +using namespace mlir; |
| 38 | + |
| 39 | +namespace { |
| 40 | +struct Options { |
| 41 | + llvm::cl::OptionCategory runnerCategory{"GPU runner options"}; |
| 42 | + llvm::cl::opt<std::string> inputFilename{ |
| 43 | + llvm::cl::Positional, llvm::cl::desc("<input file>"), llvm::cl::init("-"), |
| 44 | + llvm::cl::cat(runnerCategory)}; |
| 45 | + llvm::cl::opt<std::string> mainFuncName{ |
| 46 | + "e", |
| 47 | + llvm::cl::desc("The function to be executed. If not specified, the " |
| 48 | + "first matching function in the module to be used."), |
| 49 | + llvm::cl::value_desc("function name"), llvm::cl::cat(runnerCategory)}; |
| 50 | + llvm::cl::opt<bool> skipPipeline{ |
| 51 | + "skip-pipeline", |
| 52 | + llvm::cl::desc("Skip the GPU pipeline. It's expected, that the input is " |
| 53 | + "already lowered with 'gc-op --gc-gpu-pipeline'."), |
| 54 | + llvm::cl::init(false), llvm::cl::cat(runnerCategory)}; |
| 55 | + llvm::cl::list<std::string> sharedLibs{ |
| 56 | + "shared-libs", |
| 57 | + llvm::cl::desc("Comma separated library paths to link dynamically."), |
| 58 | + llvm::cl::MiscFlags::CommaSeparated, llvm::cl::desc("<lib1,lib2,...>"), |
| 59 | + llvm::cl::cat(runnerCategory)}; |
| 60 | + llvm::cl::opt<bool> printIr{ |
| 61 | + "print-ir", llvm::cl::desc("Print the IR before the execution."), |
| 62 | + llvm::cl::init(false), llvm::cl::cat(runnerCategory)}; |
| 63 | + llvm::cl::opt<std::string> objDumpFile{ |
| 64 | + "obj-dump-file", |
| 65 | + llvm::cl::desc("Dump the compiled object to the specified file."), |
| 66 | + llvm::cl::value_desc("file path"), llvm::cl::cat(runnerCategory)}; |
| 67 | +}; |
| 68 | +} // namespace |
| 69 | + |
| 70 | +void findFunc(Options &opts, ModuleOp mod) { |
| 71 | + bool (*matcher)(ArrayRef<Type>, ModuleOp &); |
| 72 | + |
| 73 | + if (opts.skipPipeline) { |
| 74 | + matcher = [](ArrayRef<Type> args, ModuleOp &mod) { |
| 75 | + if (args.size() != 3) |
| 76 | + return false; |
| 77 | + auto ctx = mod.getContext(); |
| 78 | + auto ptrType = LLVM::LLVMPointerType::get(ctx); |
| 79 | + return args[0] == ptrType && args[1] == ptrType && |
| 80 | + args[2] == IntegerType::get(ctx, 64); |
| 81 | + }; |
| 82 | + } else { |
| 83 | + matcher = [](ArrayRef<Type> args, ModuleOp &) { return args.empty(); }; |
| 84 | + } |
| 85 | + |
| 86 | + if (opts.mainFuncName.empty()) { |
| 87 | + auto setFuncName = [&](auto funcOp) { |
| 88 | + if (funcOp && !funcOp.isExternal() && funcOp.isPublic() && |
| 89 | + matcher(funcOp.getArgumentTypes(), mod)) { |
| 90 | + opts.mainFuncName = funcOp.getName().str(); |
| 91 | + return true; |
| 92 | + } |
| 93 | + return false; |
| 94 | + }; |
| 95 | + |
| 96 | + for (auto &op : mod.getBody()->getOperations()) { |
| 97 | + if (setFuncName(dyn_cast<LLVM::LLVMFuncOp>(op)) || |
| 98 | + setFuncName(dyn_cast<func::FuncOp>(op))) { |
| 99 | + return; |
| 100 | + } |
| 101 | + } |
| 102 | + gcReportErr("No matching function found."); |
| 103 | + } |
| 104 | + |
| 105 | + ArrayRef<Type> args; |
| 106 | + if (auto llvmFunc = mod.lookupSymbol<LLVM::LLVMFuncOp>(opts.mainFuncName)) { |
| 107 | + args = llvmFunc.getArgumentTypes(); |
| 108 | + } else if (auto func = mod.lookupSymbol<func::FuncOp>(opts.mainFuncName)) { |
| 109 | + args = func.getArgumentTypes(); |
| 110 | + } else { |
| 111 | + gcReportErr("The function '", opts.mainFuncName.c_str(), "' not found."); |
| 112 | + } |
| 113 | + |
| 114 | + if (!matcher(args, mod)) { |
| 115 | + if (opts.skipPipeline) { |
| 116 | + gcReportErr("The function '", opts.mainFuncName.c_str(), |
| 117 | + "' signature does not match (!llvm.ptr, !llvm.ptr, i64)."); |
| 118 | + } |
| 119 | + gcReportErr("The function '", opts.mainFuncName.c_str(), |
| 120 | + "' must have no arguments."); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +void pipeline(OpPassManager &pm) { |
| 125 | + gc::GPUPipelineOption pipelineOpts; |
| 126 | + pipelineOpts.isUsmArgs = false; |
| 127 | + pipelineOpts.callFinish = true; |
| 128 | + populateGPUPipeline(pm, pipelineOpts); |
| 129 | +} |
| 130 | + |
| 131 | +int main(int argc, char **argv) { |
| 132 | + Options opts; |
| 133 | + llvm::cl::ParseCommandLineOptions(argc, argv, "GraphCompiler GPU runner\n"); |
| 134 | + |
| 135 | + std::string errMsg; |
| 136 | + auto file = openInputFile(opts.inputFilename, &errMsg); |
| 137 | + if (!file) { |
| 138 | + gcReportErr("Failed to read input IR: ", errMsg.c_str()); |
| 139 | + } |
| 140 | + |
| 141 | + auto srcMgr = std::make_shared<llvm::SourceMgr>(); |
| 142 | + srcMgr->AddNewSourceBuffer(std::move(file), SMLoc()); |
| 143 | + MLIRContext mlirCtx{gc::initCompilerAndGetDialects()}; |
| 144 | + auto mlirMod = parseSourceFile<ModuleOp>(srcMgr, {&mlirCtx}); |
| 145 | + findFunc(opts, *mlirMod); |
| 146 | + |
| 147 | + gc::gpu::OclModuleBuilderOpts builderOpts; |
| 148 | + SmallVector<StringRef, 4> sharedLibs(opts.sharedLibs.begin(), |
| 149 | + opts.sharedLibs.end()); |
| 150 | + builderOpts.funcName = opts.mainFuncName; |
| 151 | + builderOpts.enableObjectDump = !opts.objDumpFile.getValue().empty(); |
| 152 | + builderOpts.sharedLibPaths = sharedLibs; |
| 153 | + if (opts.skipPipeline) { |
| 154 | + builderOpts.pipeline = |
| 155 | + opts.printIr |
| 156 | + ? [](OpPassManager &pm) { pm.addPass(createPrintIRPass()); } |
| 157 | + : [](OpPassManager &) {}; |
| 158 | + } else { |
| 159 | + builderOpts.pipeline = opts.printIr ? [](OpPassManager &pm) { |
| 160 | + pipeline(pm); |
| 161 | + pm.addPass(createPrintIRPass()); |
| 162 | + } : pipeline; |
| 163 | + } |
| 164 | + |
| 165 | + gc::gpu::OclModuleBuilder builder{mlirMod, builderOpts}; |
| 166 | + auto runtime = gcGetOrReport(gc::gpu::OclRuntime::get()); |
| 167 | + auto oclMod = gcGetOrReport(builder.build(runtime)); |
| 168 | + assert(oclMod->isStatic); |
| 169 | + |
| 170 | + if (!opts.objDumpFile.getValue().empty()) { |
| 171 | + gcLogD("Dumping the compiled object to ", opts.objDumpFile.getValue()); |
| 172 | + oclMod->dumpToObjectFile(opts.objDumpFile.getValue()); |
| 173 | + } |
| 174 | + |
| 175 | + auto queue = gcGetOrReport(runtime.createQueue()); |
| 176 | + gc::gpu::OclContext ctx{runtime, queue}; |
| 177 | + gc::gpu::StaticExecutor<0> exec{oclMod}; |
| 178 | + gcLogD("Executing function ", opts.mainFuncName.c_str(), "()"); |
| 179 | + exec(ctx); |
| 180 | + gcGetOrReport(ctx.finish()); |
| 181 | + gcGetOrReport(runtime.releaseQueue(queue)); |
| 182 | + return 0; |
| 183 | +} |
0 commit comments