|
| 1 | +//===- DialectBuilder.cpp - Dialect Builder -------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, 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 | +// This file implements builders for several dialects operations. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "mlir/Conversion/SYCLToLLVM/DialectBuilder.h" |
| 14 | +#include "mlir/IR/BuiltinTypes.h" |
| 15 | +#include "llvm/ADT/TypeSwitch.h" |
| 16 | +#include "llvm/Support/Debug.h" |
| 17 | +#include <cassert> |
| 18 | + |
| 19 | +using namespace mlir; |
| 20 | +using namespace mlir::sycl; |
| 21 | + |
| 22 | +#define DEBUG_TYPE "dialect-builder" |
| 23 | + |
| 24 | +//===----------------------------------------------------------------------===// |
| 25 | +// DialectBuilder |
| 26 | +//===----------------------------------------------------------------------===// |
| 27 | + |
| 28 | +DialectBuilder::~DialectBuilder() {} |
| 29 | + |
| 30 | +template<typename OP, typename... Types> |
| 31 | +OP DialectBuilder::create(Types... args) const { |
| 32 | + return builder.create<OP>(loc, args...); |
| 33 | +} |
| 34 | + |
| 35 | +FlatSymbolRefAttr DialectBuilder::getOrInsertFuncDecl(StringRef funcName, |
| 36 | + Type resultType, |
| 37 | + ArrayRef<Type> argsTypes, |
| 38 | + ModuleOp &module, |
| 39 | + bool isVarArg) const { |
| 40 | + if (module.lookupSymbol<LLVM::LLVMFuncOp>(funcName)) |
| 41 | + return SymbolRefAttr::get(builder.getContext(), funcName); |
| 42 | + |
| 43 | + OpBuilder::InsertionGuard guard(builder); |
| 44 | + builder.setInsertionPointToStart(module.getBody()); |
| 45 | + auto funcType = LLVM::LLVMFunctionType::get(resultType, argsTypes, isVarArg); |
| 46 | + builder.create<LLVM::LLVMFuncOp>(module.getLoc(), funcName, funcType); |
| 47 | + return SymbolRefAttr::get(builder.getContext(), funcName); |
| 48 | +} |
| 49 | + |
| 50 | +BoolAttr DialectBuilder::getBoolAttr(bool val) const { |
| 51 | + return builder.getBoolAttr(val); |
| 52 | +} |
| 53 | + |
| 54 | +IntegerAttr DialectBuilder::getIntegerAttr(Type type, int64_t val) const { |
| 55 | + return builder.getIntegerAttr(type, val); |
| 56 | +} |
| 57 | + |
| 58 | +IntegerAttr DialectBuilder::getIntegerAttr(Type type, APInt val) const { |
| 59 | + return builder.getIntegerAttr(type, val); |
| 60 | +} |
| 61 | + |
| 62 | +FloatAttr DialectBuilder::getF16FloatAttr(float val) const { |
| 63 | + return builder.getF16FloatAttr(val); |
| 64 | +} |
| 65 | + |
| 66 | +FloatAttr DialectBuilder::getF32FloatAttr(float val) const { |
| 67 | + return builder.getF32FloatAttr(val); |
| 68 | +} |
| 69 | + |
| 70 | +FloatAttr DialectBuilder::getF64FloatAttr(double val) const { |
| 71 | + return builder.getF64FloatAttr(val); |
| 72 | +} |
| 73 | + |
| 74 | +ArrayAttr DialectBuilder::getI64ArrayAttr(ArrayRef<int64_t> vals) const { |
| 75 | + return builder.getI64ArrayAttr(vals); |
| 76 | +} |
| 77 | + |
| 78 | +//===----------------------------------------------------------------------===// |
| 79 | +// MemRefBuilder |
| 80 | +//===----------------------------------------------------------------------===// |
| 81 | + |
| 82 | +memref::AllocOp MemRefBuilder::genAlloc(MemRefType type) const { |
| 83 | + return create<memref::AllocOp>(type); |
| 84 | +} |
| 85 | + |
| 86 | +memref::AllocaOp MemRefBuilder::genAlloca(MemRefType type) const { |
| 87 | + return create<memref::AllocaOp>(type); |
| 88 | +} |
| 89 | + |
| 90 | +memref::CastOp MemRefBuilder::genCast(Value input, |
| 91 | + MemRefType outputType) const { |
| 92 | + return create<memref::CastOp>(outputType, input); |
| 93 | +} |
| 94 | + |
| 95 | +memref::DeallocOp MemRefBuilder::genDealloc(Value val) const { |
| 96 | + return create<memref::DeallocOp>(val); |
| 97 | +} |
| 98 | + |
| 99 | +//===----------------------------------------------------------------------===// |
| 100 | +// LLVMBuilder |
| 101 | +//===----------------------------------------------------------------------===// |
| 102 | + |
| 103 | +LLVM::AllocaOp LLVMBuilder::genAlloca(Type type, Value size, |
| 104 | + int64_t align) const { |
| 105 | + return create<LLVM::AllocaOp>(type, size, align); |
| 106 | +} |
| 107 | + |
| 108 | +LLVM::BitcastOp LLVMBuilder::genBitcast(Type type, Value val) const { |
| 109 | + return create<LLVM::BitcastOp>(type, val); |
| 110 | +} |
| 111 | + |
| 112 | +LLVM::ExtractValueOp LLVMBuilder::genExtractValue(Type type, Value container, |
| 113 | + ArrayRef<int64_t> position) const { |
| 114 | + return create<LLVM::ExtractValueOp>(type, container, |
| 115 | + getI64ArrayAttr(position)); |
| 116 | +} |
| 117 | + |
| 118 | +LLVM::CallOp LLVMBuilder::genCall(FlatSymbolRefAttr funcSym, ArrayRef<Type> resTypes, |
| 119 | + ArrayRef<Value> operands) const { |
| 120 | + return create<LLVM::CallOp>(resTypes, funcSym, operands); |
| 121 | +} |
| 122 | + |
| 123 | +LLVM::ConstantOp LLVMBuilder::genConstant(Type type, double val) const { |
| 124 | + return llvm::TypeSwitch<Type, LLVM::ConstantOp>(type) |
| 125 | + .Case<IndexType>([&](IndexType type) { |
| 126 | + return create<LLVM::ConstantOp>(type, |
| 127 | + getIntegerAttr(type, (int64_t)val)); |
| 128 | + }) |
| 129 | + .Case<IntegerType>([&](IntegerType type) { |
| 130 | + bool isBool = (type.getWidth() == 1); |
| 131 | + return (isBool) ? create<LLVM::ConstantOp>(type, getBoolAttr(val != 0)) |
| 132 | + : create<LLVM::ConstantOp>( |
| 133 | + type, getIntegerAttr(type, APInt(type.getWidth(), |
| 134 | + (int64_t)val))); |
| 135 | + }) |
| 136 | + .Case<Float16Type>([&](Type) { |
| 137 | + return create<LLVM::ConstantOp>(type, getF16FloatAttr(val)); |
| 138 | + }) |
| 139 | + .Case<Float32Type>([&](Type) { |
| 140 | + return create<LLVM::ConstantOp>(type, getF32FloatAttr(val)); |
| 141 | + }) |
| 142 | + .Case<Float64Type>([&](Type) { |
| 143 | + return create<LLVM::ConstantOp>(type, getF64FloatAttr(val)); |
| 144 | + }) |
| 145 | + .Default([&](Type) { |
| 146 | + llvm_unreachable("Missing support for type"); |
| 147 | + return LLVM::ConstantOp(); |
| 148 | + }); |
| 149 | +} |
| 150 | + |
| 151 | +LLVM::SExtOp LLVMBuilder::genSignExtend(Type type, Value val) const { |
| 152 | + return create<LLVM::SExtOp>(type, val); |
| 153 | +} |
0 commit comments