|
| 1 | +//===-- JitWrapper.cpp - Wrapper for JIT ------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This file is licensed under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "gc/ExecutionEngine/Driver/Driver.h" |
| 10 | +#include "mlir/AsmParser/AsmParser.h" |
| 11 | +#include "mlir/ExecutionEngine/MemRefUtils.h" |
| 12 | +#include "mlir/IR/AsmState.h" |
| 13 | +#include "mlir/IR/BuiltinOps.h" |
| 14 | +#include "mlir/IR/MLIRContext.h" |
| 15 | +#include "mlir/Parser/Parser.h" |
| 16 | +#include "mlir/Pass/PassManager.h" |
| 17 | +#include "llvm/Support/ErrorOr.h" |
| 18 | +#include "llvm/Support/MemoryBuffer.h" |
| 19 | +#include "llvm/Support/SourceMgr.h" |
| 20 | +#include "llvm/Support/raw_ostream.h" |
| 21 | +#include "gtest/gtest.h" |
| 22 | +#include <memory> |
| 23 | + |
| 24 | +using namespace mlir; |
| 25 | + |
| 26 | +static const char code1[] = R"mlir( |
| 27 | +module { |
| 28 | +llvm.mlir.global constant @__num_orig_num_args(3 : i32) : i32 |
| 29 | +func.func @compute(%a: tensor<128xf32>, %b: tensor<128xf32>) -> tensor<128xf32> attributes { llvm.emit_c_interface } { |
| 30 | + %out = tensor.empty() : tensor<128xf32> |
| 31 | + %2 = linalg.add ins(%a, %b : tensor<128xf32>,tensor<128xf32>) outs(%out : tensor<128xf32>) -> tensor<128xf32> |
| 32 | + return %2 : tensor<128xf32> |
| 33 | +} |
| 34 | +} |
| 35 | +)mlir"; |
| 36 | + |
| 37 | +extern "C" { |
| 38 | +extern int gc_runtime_keep_alive; |
| 39 | +} |
| 40 | + |
| 41 | +TEST(ExecutionEngine, JitWrapper) { |
| 42 | + gc_runtime_keep_alive = 0; |
| 43 | + MLIRContext ctx{gc::initCompilerAndGetDialects()}; |
| 44 | + std::unique_ptr<llvm::MemoryBuffer> ir_buffer = |
| 45 | + llvm::MemoryBuffer::getMemBuffer(code1); |
| 46 | + // Parse the input mlir. |
| 47 | + llvm::SourceMgr sourceMgr; |
| 48 | + sourceMgr.AddNewSourceBuffer(std::move(ir_buffer), llvm::SMLoc()); |
| 49 | + mlir::OwningOpRef<mlir::ModuleOp> module = |
| 50 | + mlir::parseSourceFile<mlir::ModuleOp>(sourceMgr, &ctx); |
| 51 | + ASSERT_TRUE(module); |
| 52 | + auto jited = gc::JitModule::create(module.get()); |
| 53 | + bool jit_success = static_cast<bool>(jited); |
| 54 | + if (!jit_success) { |
| 55 | + auto err = jited.takeError(); |
| 56 | + llvm::errs() << err; |
| 57 | + llvm::consumeError(std::move(err)); |
| 58 | + } |
| 59 | + ASSERT_TRUE(jit_success); |
| 60 | + OwningMemRef<float, 1> bufA{ |
| 61 | + {128}, {128}, [](float &ptr, ArrayRef<int64_t>) { ptr = 1.0f; }}; |
| 62 | + OwningMemRef<float, 1> bufB{ |
| 63 | + {128}, {128}, [](float &ptr, ArrayRef<int64_t> idx) { ptr = idx[0]; }}; |
| 64 | + OwningMemRef<float, 1> bufC{{128}, {128}}; |
| 65 | + void *args[] = {&*bufA, &*bufB, &*bufC}; |
| 66 | + jited.get()->call(args, 3); |
| 67 | + for (int i = 0; i < 128; i++) { |
| 68 | + ASSERT_EQ(bufC[{i}], 1.0f + i); |
| 69 | + } |
| 70 | +} |
0 commit comments