MLIR-AIE
XLLVMOps.cpp
Go to the documentation of this file.
1//===---- XLLVMOps.cpp - XLLVM Dialect Operations ---------------*- 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// (c) Copyright 2024 Advanced Micro Devices, Inc.
8//
9//===----------------------------------------------------------------------===//
10// External LLVM (XLLVM) Dialect implementation.
11//===----------------------------------------------------------------------===//
12
14#include "llvm/IR/IRBuilder.h"
15#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
16#include "mlir/IR/OpDefinition.h"
17#include "mlir/IR/TypeUtilities.h"
18#include "mlir/Target/LLVMIR/ModuleTranslation.h"
19#include "mlir/Transforms/FoldUtils.h"
20
21using namespace mlir;
22using namespace xilinx;
23using namespace xilinx::xllvm;
24
25#include "aie/Dialect/XLLVM/IR/XLLVMDialect.cpp.inc"
26
27//===----------------------------------------------------------------------===//
28// XLLVMDialect
29//===----------------------------------------------------------------------===//
30
31void xllvm::XLLVMDialect::initialize() {
32 addOperations<
33#define GET_OP_LIST
34#include "aie/Dialect/XLLVM/IR/XLLVMOps.cpp.inc"
35 >();
36}
37
38namespace xilinx::xllvm {
39
40static llvm::Function *
41getNamedIntrinsicDeclaration(llvm::Module *M, llvm::StringRef fullName,
42 llvm::Type *resTy, ArrayRef<llvm::Type *> argsTy) {
43 auto *FT = llvm::FunctionType::get(resTy, argsTy, /*isVarArg=*/false);
44 return cast<llvm::Function>(M->getOrInsertFunction(fullName, FT).getCallee());
45}
46
48 llvm::IRBuilderBase &builder, LLVM::ModuleTranslation &moduleTranslation,
49 Operation *intrOp, llvm::StringRef intrinsicName) {
50 llvm::Type *resTy = nullptr;
51 unsigned numResults = intrOp->getNumResults();
52 if (numResults == 1)
53 resTy = moduleTranslation.convertType(*(intrOp->getResultTypes().begin()));
54 else if (numResults > 1) {
55 SmallVector<llvm::Type *> resTys;
56 for (auto ty : intrOp->getResultTypes())
57 resTys.push_back(moduleTranslation.convertType(ty));
58 resTy = llvm::StructType::get(builder.getContext(), resTys);
59 }
60 auto operands = moduleTranslation.lookupValues(intrOp->getOperands());
61 SmallVector<llvm::Type *> types;
62 for (auto op : operands)
63 types.push_back(op->getType());
64 llvm::Module *module = builder.GetInsertBlock()->getModule();
65 llvm::Function *llvmIntr =
66 getNamedIntrinsicDeclaration(module, intrinsicName, resTy, types);
67 return builder.CreateCall(llvmIntr, operands);
68}
69
70} // namespace xilinx::xllvm
71
72#define GET_OP_CLASSES
73#include "aie/Dialect/XLLVM/IR/XLLVMOps.cpp.inc"
llvm::CallInst * createExternalLLVMIntrinsicCall(llvm::IRBuilderBase &builder, mlir::LLVM::ModuleTranslation &moduleTranslation, mlir::Operation *intrOp, llvm::StringRef intrinsicName)