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