| 1 |
//===-- TargetRewrite.cpp -------------------------------------------------===// |
| 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 |
// Target rewrite: rewriting of ops to make target-specific lowerings manifest. |
| 10 |
// LLVM expects different lowering idioms to be used for distinct target |
| 11 |
// triples. These distinctions are handled by this pass. |
| 12 |
// |
| 13 |
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ |
| 14 |
// |
| 15 |
//===----------------------------------------------------------------------===// |
| 16 |
|
| 17 |
#include "flang/Optimizer/CodeGen/CodeGen.h" |
| 18 |
|
| 19 |
#include "flang/Optimizer/Builder/Character.h" |
| 20 |
#include "flang/Optimizer/Builder/FIRBuilder.h" |
| 21 |
#include "flang/Optimizer/Builder/Todo.h" |
| 22 |
#include "flang/Optimizer/CodeGen/Target.h" |
| 23 |
#include "flang/Optimizer/Dialect/FIRDialect.h" |
| 24 |
#include "flang/Optimizer/Dialect/FIROps.h" |
| 25 |
#include "flang/Optimizer/Dialect/FIROpsSupport.h" |
| 26 |
#include "flang/Optimizer/Dialect/FIRType.h" |
| 27 |
#include "flang/Optimizer/Dialect/Support/FIRContext.h" |
| 28 |
#include "flang/Optimizer/Support/DataLayout.h" |
| 29 |
#include "mlir/Dialect/DLTI/DLTI.h" |
| 30 |
#include "mlir/Transforms/DialectConversion.h" |
| 31 |
#include "llvm/ADT/STLExtras.h" |
| 32 |
#include "llvm/ADT/TypeSwitch.h" |
| 33 |
#include "llvm/Support/Debug.h" |
| 34 |
#include <optional> |
| 35 |
|
| 36 |
namespace fir { |
| 37 |
#define GEN_PASS_DEF_TARGETREWRITEPASS |
| 38 |
#include "flang/Optimizer/CodeGen/CGPasses.h.inc" |
| 39 |
} // namespace fir |
| 40 |
|
| 41 |
#define DEBUG_TYPE "flang-target-rewrite" |
| 42 |
|
| 43 |
namespace { |
| 44 |
|
| 45 |
/// Fixups for updating a FuncOp's arguments and return values. |
| 46 |
struct FixupTy { |
| 47 |
enum class Codes { |
| 48 |
ArgumentAsLoad, |
| 49 |
ArgumentType, |
| 50 |
CharPair, |
| 51 |
ReturnAsStore, |
| 52 |
ReturnType, |
| 53 |
Split, |
| 54 |
Trailing, |
| 55 |
TrailingCharProc |
| 56 |
}; |
| 57 |
|
| 58 |
FixupTy(Codes code, std::size_t index, std::size_t second = 0) |
| 59 |
: code{code}, index{index}, second{second} {} |
| 60 |
FixupTy(Codes code, std::size_t index, |
| 61 |
std::function<void(mlir::func::FuncOp)> &&finalizer) |
| 62 |
: code{code}, index{index}, finalizer{finalizer} {} |
| 63 |
FixupTy(Codes code, std::size_t index, std::size_t second, |
| 64 |
std::function<void(mlir::func::FuncOp)> &&finalizer) |
| 65 |
: code{code}, index{index}, second{second}, finalizer{finalizer} {} |
| 66 |
|
| 67 |
Codes code; |
| 68 |
std::size_t index; |
| 69 |
std::size_t second{}; |
| 70 |
std::optional<std::function<void(mlir::func::FuncOp)>> finalizer{}; |
| 71 |
}; // namespace |
| 72 |
|
| 73 |
/// Target-specific rewriting of the FIR. This is a prerequisite pass to code |
| 74 |
/// generation that traverses the FIR and modifies types and operations to a |
| 75 |
/// form that is appropriate for the specific target. LLVM IR has specific |
| 76 |
/// idioms that are used for distinct target processor and ABI combinations. |
| 77 |
class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> { |
| 78 |
public: |
| 79 |
using TargetRewritePassBase<TargetRewrite>::TargetRewritePassBase; |
| 80 |
|
| 81 |
void runOnOperation() override final { |
| 82 |
auto &context = getContext(); |
| 83 |
mlir::OpBuilder rewriter(&context); |
| 84 |
|
| 85 |
auto mod = getModule(); |
| 86 |
if (!forcedTargetTriple.empty()) |
| 87 |
fir::setTargetTriple(mod, forcedTargetTriple); |
| 88 |
|
| 89 |
if (!forcedTargetCPU.empty()) |
| 90 |
fir::setTargetCPU(mod, forcedTargetCPU); |
| 91 |
|
| 92 |
if (!forcedTuneCPU.empty()) |
| 93 |
fir::setTuneCPU(mod, forcedTuneCPU); |
| 94 |
|
| 95 |
if (!forcedTargetFeatures.empty()) |
| 96 |
fir::setTargetFeatures(mod, forcedTargetFeatures); |
| 97 |
|
| 98 |
// TargetRewrite will require querying the type storage sizes, if it was |
| 99 |
// not set already, create a DataLayoutSpec for the ModuleOp now. |
| 100 |
std::optional<mlir::DataLayout> dl = |
| 101 |
fir::support::getOrSetDataLayout(mod, /*allowDefaultLayout=*/true); |
| 102 |
if (!dl) { |
| 103 |
mlir::emitError(mod.getLoc(), |
| 104 |
"module operation must carry a data layout attribute " |
| 105 |
"to perform target ABI rewrites on FIR"); |
| 106 |
signalPassFailure(); |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
auto specifics = fir::CodeGenSpecifics::get( |
| 111 |
mod.getContext(), fir::getTargetTriple(mod), fir::getKindMapping(mod), |
| 112 |
fir::getTargetCPU(mod), fir::getTargetFeatures(mod), *dl, |
| 113 |
fir::getTuneCPU(mod)); |
| 114 |
|
| 115 |
setMembers(specifics.get(), &rewriter, &*dl); |
| 116 |
|
| 117 |
// We may need to call stacksave/stackrestore later, so |
| 118 |
// create the FuncOps beforehand. |
| 119 |
fir::FirOpBuilder builder(rewriter, mod); |
| 120 |
builder.setInsertionPointToStart(mod.getBody()); |
| 121 |
stackSaveFn = fir::factory::getLlvmStackSave(builder); |
| 122 |
stackRestoreFn = fir::factory::getLlvmStackRestore(builder); |
| 123 |
|
| 124 |
// Perform type conversion on signatures and call sites. |
| 125 |
if (mlir::failed(convertTypes(mod))) { |
| 126 |
mlir::emitError(mlir::UnknownLoc::get(&context), |
| 127 |
"error in converting types to target abi"); |
| 128 |
signalPassFailure(); |
| 129 |
} |
| 130 |
|
| 131 |
// Convert ops in target-specific patterns. |
| 132 |
mod.walk([&](mlir::Operation *op) { |
| 133 |
if (auto call = mlir::dyn_cast<fir::CallOp>(op)) { |
| 134 |
if (!hasPortableSignature(call.getFunctionType(), op)) |
| 135 |
convertCallOp(call); |
| 136 |
} else if (auto dispatch = mlir::dyn_cast<fir::DispatchOp>(op)) { |
| 137 |
if (!hasPortableSignature(dispatch.getFunctionType(), op)) |
| 138 |
convertCallOp(dispatch); |
| 139 |
} else if (auto addr = mlir::dyn_cast<fir::AddrOfOp>(op)) { |
| 140 |
if (mlir::isa<mlir::FunctionType>(addr.getType()) && |
| 141 |
!hasPortableSignature(addr.getType(), op)) |
| 142 |
convertAddrOp(addr); |
| 143 |
} |
| 144 |
}); |
| 145 |
|
| 146 |
clearMembers(); |
| 147 |
} |
| 148 |
|
| 149 |
mlir::ModuleOp getModule() { return getOperation(); } |
| 150 |
|
| 151 |
template <typename A, typename B, typename C> |
| 152 |
std::optional<std::function<mlir::Value(mlir::Operation *)>> |
| 153 |
rewriteCallComplexResultType( |
| 154 |
mlir::Location loc, A ty, B &newResTys, |
| 155 |
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs, C &newOpers, |
| 156 |
mlir::Value &savedStackPtr) { |
| 157 |
if (noComplexConversion) { |
| 158 |
newResTys.push_back(ty); |
| 159 |
return std::nullopt; |
| 160 |
} |
| 161 |
auto m = specifics->complexReturnType(loc, ty.getElementType()); |
| 162 |
// Currently targets mandate COMPLEX is a single aggregate or packed |
| 163 |
// scalar, including the sret case. |
| 164 |
assert(m.size() == 1 && "target of complex return not supported"); |
| 165 |
auto resTy = std::get<mlir::Type>(m[0]); |
| 166 |
auto attr = std::get<fir::CodeGenSpecifics::Attributes>(m[0]); |
| 167 |
if (attr.isSRet()) { |
| 168 |
assert(fir::isa_ref_type(resTy) && "must be a memory reference type"); |
| 169 |
// Save the stack pointer, if it has not been saved for this call yet. |
| 170 |
// We will need to restore it after the call, because the alloca |
| 171 |
// needs to be deallocated. |
| 172 |
if (!savedStackPtr) |
| 173 |
savedStackPtr = genStackSave(loc); |
| 174 |
mlir::Value stack = |
| 175 |
rewriter->create<fir::AllocaOp>(loc, fir::dyn_cast_ptrEleTy(resTy)); |
| 176 |
newInTyAndAttrs.push_back(m[0]); |
| 177 |
newOpers.push_back(stack); |
| 178 |
return [=](mlir::Operation *) -> mlir::Value { |
| 179 |
auto memTy = fir::ReferenceType::get(ty); |
| 180 |
auto cast = rewriter->create<fir::ConvertOp>(loc, memTy, stack); |
| 181 |
return rewriter->create<fir::LoadOp>(loc, cast); |
| 182 |
}; |
| 183 |
} |
| 184 |
newResTys.push_back(resTy); |
| 185 |
return [=, &savedStackPtr](mlir::Operation *call) -> mlir::Value { |
| 186 |
// We are going to generate an alloca, so save the stack pointer. |
| 187 |
if (!savedStackPtr) |
| 188 |
savedStackPtr = genStackSave(loc); |
| 189 |
return this->convertValueInMemory(loc, call->getResult(0), ty, |
| 190 |
/*inputMayBeBigger=*/true); |
| 191 |
}; |
| 192 |
} |
| 193 |
|
| 194 |
void passArgumentOnStackOrWithNewType( |
| 195 |
mlir::Location loc, fir::CodeGenSpecifics::TypeAndAttr newTypeAndAttr, |
| 196 |
mlir::Type oldType, mlir::Value oper, |
| 197 |
llvm::SmallVectorImpl<mlir::Value> &newOpers, |
| 198 |
mlir::Value &savedStackPtr) { |
| 199 |
auto resTy = std::get<mlir::Type>(newTypeAndAttr); |
| 200 |
auto attr = std::get<fir::CodeGenSpecifics::Attributes>(newTypeAndAttr); |
| 201 |
// We are going to generate an alloca, so save the stack pointer. |
| 202 |
if (!savedStackPtr) |
| 203 |
savedStackPtr = genStackSave(loc); |
| 204 |
if (attr.isByVal()) { |
| 205 |
mlir::Value mem = rewriter->create<fir::AllocaOp>(loc, oldType); |
| 206 |
rewriter->create<fir::StoreOp>(loc, oper, mem); |
| 207 |
if (mem.getType() != resTy) |
| 208 |
mem = rewriter->create<fir::ConvertOp>(loc, resTy, mem); |
| 209 |
newOpers.push_back(mem); |
| 210 |
} else { |
| 211 |
mlir::Value bitcast = |
| 212 |
convertValueInMemory(loc, oper, resTy, /*inputMayBeBigger=*/false); |
| 213 |
newOpers.push_back(bitcast); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
// Do a bitcast (convert a value via its memory representation). |
| 218 |
// The input and output types may have different storage sizes, |
| 219 |
// "inputMayBeBigger" should be set to indicate which of the input or |
| 220 |
// output type may be bigger in order for the load/store to be safe. |
| 221 |
// The mismatch comes from the fact that the LLVM register used for passing |
| 222 |
// may be bigger than the value being passed (e.g., passing |
| 223 |
// a `!fir.type<t{fir.array<3xi8>}>` into an i32 LLVM register). |
| 224 |
mlir::Value convertValueInMemory(mlir::Location loc, mlir::Value value, |
| 225 |
mlir::Type newType, bool inputMayBeBigger) { |
| 226 |
if (inputMayBeBigger) { |
| 227 |
auto newRefTy = fir::ReferenceType::get(newType); |
| 228 |
auto mem = rewriter->create<fir::AllocaOp>(loc, value.getType()); |
| 229 |
rewriter->create<fir::StoreOp>(loc, value, mem); |
| 230 |
auto cast = rewriter->create<fir::ConvertOp>(loc, newRefTy, mem); |
| 231 |
return rewriter->create<fir::LoadOp>(loc, cast); |
| 232 |
} else { |
| 233 |
auto oldRefTy = fir::ReferenceType::get(value.getType()); |
| 234 |
auto mem = rewriter->create<fir::AllocaOp>(loc, newType); |
| 235 |
auto cast = rewriter->create<fir::ConvertOp>(loc, oldRefTy, mem); |
| 236 |
rewriter->create<fir::StoreOp>(loc, value, cast); |
| 237 |
return rewriter->create<fir::LoadOp>(loc, mem); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
void passSplitArgument(mlir::Location loc, |
| 242 |
fir::CodeGenSpecifics::Marshalling splitArgs, |
| 243 |
mlir::Type oldType, mlir::Value oper, |
| 244 |
llvm::SmallVectorImpl<mlir::Value> &newOpers, |
| 245 |
mlir::Value &savedStackPtr) { |
| 246 |
// COMPLEX or struct argument split into separate arguments |
| 247 |
if (!fir::isa_complex(oldType)) { |
| 248 |
// Cast original operand to a tuple of the new arguments |
| 249 |
// via memory. |
| 250 |
llvm::SmallVector<mlir::Type> partTypes; |
| 251 |
for (auto argPart : splitArgs) |
| 252 |
partTypes.push_back(std::get<mlir::Type>(argPart)); |
| 253 |
mlir::Type tupleType = |
| 254 |
mlir::TupleType::get(oldType.getContext(), partTypes); |
| 255 |
if (!savedStackPtr) |
| 256 |
savedStackPtr = genStackSave(loc); |
| 257 |
oper = convertValueInMemory(loc, oper, tupleType, |
| 258 |
/*inputMayBeBigger=*/false); |
| 259 |
} |
| 260 |
auto iTy = rewriter->getIntegerType(32); |
| 261 |
for (auto e : llvm::enumerate(splitArgs)) { |
| 262 |
auto &tup = e.value(); |
| 263 |
auto ty = std::get<mlir::Type>(tup); |
| 264 |
auto index = e.index(); |
| 265 |
auto idx = rewriter->getIntegerAttr(iTy, index); |
| 266 |
auto val = rewriter->create<fir::ExtractValueOp>( |
| 267 |
loc, ty, oper, rewriter->getArrayAttr(idx)); |
| 268 |
newOpers.push_back(val); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
void rewriteCallOperands( |
| 273 |
mlir::Location loc, fir::CodeGenSpecifics::Marshalling passArgAs, |
| 274 |
mlir::Type originalArgTy, mlir::Value oper, |
| 275 |
llvm::SmallVectorImpl<mlir::Value> &newOpers, mlir::Value &savedStackPtr, |
| 276 |
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs) { |
| 277 |
if (passArgAs.size() == 1) { |
| 278 |
// COMPLEX or derived type is passed as a single argument. |
| 279 |
passArgumentOnStackOrWithNewType(loc, passArgAs[0], originalArgTy, oper, |
| 280 |
newOpers, savedStackPtr); |
| 281 |
} else { |
| 282 |
// COMPLEX or derived type is split into separate arguments |
| 283 |
passSplitArgument(loc, passArgAs, originalArgTy, oper, newOpers, |
| 284 |
savedStackPtr); |
| 285 |
} |
| 286 |
newInTyAndAttrs.insert(newInTyAndAttrs.end(), passArgAs.begin(), |
| 287 |
passArgAs.end()); |
| 288 |
} |
| 289 |
|
| 290 |
template <typename CPLX> |
| 291 |
void rewriteCallComplexInputType( |
| 292 |
mlir::Location loc, CPLX ty, mlir::Value oper, |
| 293 |
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs, |
| 294 |
llvm::SmallVectorImpl<mlir::Value> &newOpers, |
| 295 |
mlir::Value &savedStackPtr) { |
| 296 |
if (noComplexConversion) { |
| 297 |
newInTyAndAttrs.push_back(fir::CodeGenSpecifics::getTypeAndAttr(ty)); |
| 298 |
newOpers.push_back(oper); |
| 299 |
return; |
| 300 |
} |
| 301 |
auto m = specifics->complexArgumentType(loc, ty.getElementType()); |
| 302 |
rewriteCallOperands(loc, m, ty, oper, newOpers, savedStackPtr, |
| 303 |
newInTyAndAttrs); |
| 304 |
} |
| 305 |
|
| 306 |
void rewriteCallStructInputType( |
| 307 |
mlir::Location loc, fir::RecordType recTy, mlir::Value oper, |
| 308 |
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs, |
| 309 |
llvm::SmallVectorImpl<mlir::Value> &newOpers, |
| 310 |
mlir::Value &savedStackPtr) { |
| 311 |
if (noStructConversion) { |
| 312 |
newInTyAndAttrs.push_back(fir::CodeGenSpecifics::getTypeAndAttr(recTy)); |
| 313 |
newOpers.push_back(oper); |
| 314 |
return; |
| 315 |
} |
| 316 |
auto structArgs = |
| 317 |
specifics->structArgumentType(loc, recTy, newInTyAndAttrs); |
| 318 |
rewriteCallOperands(loc, structArgs, recTy, oper, newOpers, savedStackPtr, |
| 319 |
newInTyAndAttrs); |
| 320 |
} |
| 321 |
|
| 322 |
static bool hasByValOrSRetArgs( |
| 323 |
const fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs) { |
| 324 |
return llvm::any_of(newInTyAndAttrs, [](auto arg) { |
| 325 |
const auto &attr = std::get<fir::CodeGenSpecifics::Attributes>(arg); |
| 326 |
return attr.isByVal() || attr.isSRet(); |
| 327 |
}); |
| 328 |
} |
| 329 |
|
| 330 |
// Convert fir.call and fir.dispatch Ops. |
| 331 |
template <typename A> |
| 332 |
void convertCallOp(A callOp) { |
| 333 |
auto fnTy = callOp.getFunctionType(); |
| 334 |
auto loc = callOp.getLoc(); |
| 335 |
rewriter->setInsertionPoint(callOp); |
| 336 |
llvm::SmallVector<mlir::Type> newResTys; |
| 337 |
fir::CodeGenSpecifics::Marshalling newInTyAndAttrs; |
| 338 |
llvm::SmallVector<mlir::Value> newOpers; |
| 339 |
mlir::Value savedStackPtr = nullptr; |
| 340 |
|
| 341 |
// If the call is indirect, the first argument must still be the function |
| 342 |
// to call. |
| 343 |
int dropFront = 0; |
| 344 |
if constexpr (std::is_same_v<std::decay_t<A>, fir::CallOp>) { |
| 345 |
if (!callOp.getCallee()) { |
| 346 |
newInTyAndAttrs.push_back( |
| 347 |
fir::CodeGenSpecifics::getTypeAndAttr(fnTy.getInput(0))); |
| 348 |
newOpers.push_back(callOp.getOperand(0)); |
| 349 |
dropFront = 1; |
| 350 |
} |
| 351 |
} else { |
| 352 |
dropFront = 1; // First operand is the polymorphic object. |
| 353 |
} |
| 354 |
|
| 355 |
// Determine the rewrite function, `wrap`, for the result value. |
| 356 |
std::optional<std::function<mlir::Value(mlir::Operation *)>> wrap; |
| 357 |
if (fnTy.getResults().size() == 1) { |
| 358 |
mlir::Type ty = fnTy.getResult(0); |
| 359 |
llvm::TypeSwitch<mlir::Type>(ty) |
| 360 |
.template Case<fir::ComplexType>([&](fir::ComplexType cmplx) { |
| 361 |
wrap = rewriteCallComplexResultType(loc, cmplx, newResTys, |
| 362 |
newInTyAndAttrs, newOpers, |
| 363 |
savedStackPtr); |
| 364 |
}) |
| 365 |
.template Case<mlir::ComplexType>([&](mlir::ComplexType cmplx) { |
| 366 |
wrap = rewriteCallComplexResultType(loc, cmplx, newResTys, |
| 367 |
newInTyAndAttrs, newOpers, |
| 368 |
savedStackPtr); |
| 369 |
}) |
| 370 |
.Default([&](mlir::Type ty) { newResTys.push_back(ty); }); |
| 371 |
} else if (fnTy.getResults().size() > 1) { |
| 372 |
TODO(loc, "multiple results not supported yet"); |
| 373 |
} |
| 374 |
|
| 375 |
llvm::SmallVector<mlir::Type> trailingInTys; |
| 376 |
llvm::SmallVector<mlir::Value> trailingOpers; |
| 377 |
unsigned passArgShift = 0; |
| 378 |
for (auto e : llvm::enumerate( |
| 379 |
llvm::zip(fnTy.getInputs().drop_front(dropFront), |
| 380 |
callOp.getOperands().drop_front(dropFront)))) { |
| 381 |
mlir::Type ty = std::get<0>(e.value()); |
| 382 |
mlir::Value oper = std::get<1>(e.value()); |
| 383 |
unsigned index = e.index(); |
| 384 |
llvm::TypeSwitch<mlir::Type>(ty) |
| 385 |
.template Case<fir::BoxCharType>([&](fir::BoxCharType boxTy) { |
| 386 |
bool sret; |
| 387 |
if constexpr (std::is_same_v<std::decay_t<A>, fir::CallOp>) { |
| 388 |
if (noCharacterConversion) { |
| 389 |
newInTyAndAttrs.push_back( |
| 390 |
fir::CodeGenSpecifics::getTypeAndAttr(boxTy)); |
| 391 |
newOpers.push_back(oper); |
| 392 |
return; |
| 393 |
} |
| 394 |
sret = callOp.getCallee() && |
| 395 |
functionArgIsSRet( |
| 396 |
index, getModule().lookupSymbol<mlir::func::FuncOp>( |
| 397 |
*callOp.getCallee())); |
| 398 |
} else { |
| 399 |
// TODO: dispatch case; how do we put arguments on a call? |
| 400 |
// We cannot put both an sret and the dispatch object first. |
| 401 |
sret = false; |
| 402 |
TODO(loc, "dispatch + sret not supported yet"); |
| 403 |
} |
| 404 |
auto m = specifics->boxcharArgumentType(boxTy.getEleTy(), sret); |
| 405 |
auto unbox = rewriter->create<fir::UnboxCharOp>( |
| 406 |
loc, std::get<mlir::Type>(m[0]), std::get<mlir::Type>(m[1]), |
| 407 |
oper); |
| 408 |
// unboxed CHARACTER arguments |
| 409 |
for (auto e : llvm::enumerate(m)) { |
| 410 |
unsigned idx = e.index(); |
| 411 |
auto attr = |
| 412 |
std::get<fir::CodeGenSpecifics::Attributes>(e.value()); |
| 413 |
auto argTy = std::get<mlir::Type>(e.value()); |
| 414 |
if (attr.isAppend()) { |
| 415 |
trailingInTys.push_back(argTy); |
| 416 |
trailingOpers.push_back(unbox.getResult(idx)); |
| 417 |
} else { |
| 418 |
newInTyAndAttrs.push_back(e.value()); |
| 419 |
newOpers.push_back(unbox.getResult(idx)); |
| 420 |
} |
| 421 |
} |
| 422 |
}) |
| 423 |
.template Case<fir::ComplexType>([&](fir::ComplexType cmplx) { |
| 424 |
rewriteCallComplexInputType(loc, cmplx, oper, newInTyAndAttrs, |
| 425 |
newOpers, savedStackPtr); |
| 426 |
}) |
| 427 |
.template Case<mlir::ComplexType>([&](mlir::ComplexType cmplx) { |
| 428 |
rewriteCallComplexInputType(loc, cmplx, oper, newInTyAndAttrs, |
| 429 |
newOpers, savedStackPtr); |
| 430 |
}) |
| 431 |
.template Case<fir::RecordType>([&](fir::RecordType recTy) { |
| 432 |
rewriteCallStructInputType(loc, recTy, oper, newInTyAndAttrs, |
| 433 |
newOpers, savedStackPtr); |
| 434 |
}) |
| 435 |
.template Case<mlir::TupleType>([&](mlir::TupleType tuple) { |
| 436 |
if (fir::isCharacterProcedureTuple(tuple)) { |
| 437 |
mlir::ModuleOp module = getModule(); |
| 438 |
if constexpr (std::is_same_v<std::decay_t<A>, fir::CallOp>) { |
| 439 |
if (callOp.getCallee()) { |
| 440 |
llvm::StringRef charProcAttr = |
| 441 |
fir::getCharacterProcedureDummyAttrName(); |
| 442 |
// The charProcAttr attribute is only used as a safety to |
| 443 |
// confirm that this is a dummy procedure and should be split. |
| 444 |
// It cannot be used to match because attributes are not |
| 445 |
// available in case of indirect calls. |
| 446 |
auto funcOp = module.lookupSymbol<mlir::func::FuncOp>( |
| 447 |
*callOp.getCallee()); |
| 448 |
if (funcOp && |
| 449 |
!funcOp.template getArgAttrOfType<mlir::UnitAttr>( |
| 450 |
index, charProcAttr)) |
| 451 |
mlir::emitError(loc, "tuple argument will be split even " |
| 452 |
"though it does not have the `" + |
| 453 |
charProcAttr + "` attribute"); |
| 454 |
} |
| 455 |
} |
| 456 |
mlir::Type funcPointerType = tuple.getType(0); |
| 457 |
mlir::Type lenType = tuple.getType(1); |
| 458 |
fir::FirOpBuilder builder(*rewriter, module); |
| 459 |
auto [funcPointer, len] = |
| 460 |
fir::factory::extractCharacterProcedureTuple(builder, loc, |
| 461 |
oper); |
| 462 |
newInTyAndAttrs.push_back( |
| 463 |
fir::CodeGenSpecifics::getTypeAndAttr(funcPointerType)); |
| 464 |
newOpers.push_back(funcPointer); |
| 465 |
trailingInTys.push_back(lenType); |
| 466 |
trailingOpers.push_back(len); |
| 467 |
} else { |
| 468 |
newInTyAndAttrs.push_back( |
| 469 |
fir::CodeGenSpecifics::getTypeAndAttr(tuple)); |
| 470 |
newOpers.push_back(oper); |
| 471 |
} |
| 472 |
}) |
| 473 |
.Default([&](mlir::Type ty) { |
| 474 |
if constexpr (std::is_same_v<std::decay_t<A>, fir::DispatchOp>) { |
| 475 |
if (callOp.getPassArgPos() && *callOp.getPassArgPos() == index) |
| 476 |
passArgShift = newOpers.size() - *callOp.getPassArgPos(); |
| 477 |
} |
| 478 |
newInTyAndAttrs.push_back( |
| 479 |
fir::CodeGenSpecifics::getTypeAndAttr(ty)); |
| 480 |
newOpers.push_back(oper); |
| 481 |
}); |
| 482 |
} |
| 483 |
|
| 484 |
llvm::SmallVector<mlir::Type> newInTypes = toTypeList(newInTyAndAttrs); |
| 485 |
newInTypes.insert(newInTypes.end(), trailingInTys.begin(), |
| 486 |
trailingInTys.end()); |
| 487 |
newOpers.insert(newOpers.end(), trailingOpers.begin(), trailingOpers.end()); |
| 488 |
|
| 489 |
llvm::SmallVector<mlir::Value, 1> newCallResults; |
| 490 |
if constexpr (std::is_same_v<std::decay_t<A>, fir::CallOp>) { |
| 491 |
fir::CallOp newCall; |
| 492 |
if (callOp.getCallee()) { |
| 493 |
newCall = |
| 494 |
rewriter->create<A>(loc, *callOp.getCallee(), newResTys, newOpers); |
| 495 |
} else { |
| 496 |
// TODO: llvm dialect must be updated to propagate argument on |
| 497 |
// attributes for indirect calls. See: |
| 498 |
// https://discourse.llvm.org/t/should-llvm-callop-be-able-to-carry-argument-attributes-for-indirect-calls/75431 |
| 499 |
if (hasByValOrSRetArgs(newInTyAndAttrs)) |
| 500 |
TODO(loc, |
| 501 |
"passing argument or result on the stack in indirect calls"); |
| 502 |
newOpers[0].setType(mlir::FunctionType::get( |
| 503 |
callOp.getContext(), |
| 504 |
mlir::TypeRange{newInTypes}.drop_front(dropFront), newResTys)); |
| 505 |
newCall = rewriter->create<A>(loc, newResTys, newOpers); |
| 506 |
} |
| 507 |
LLVM_DEBUG(llvm::dbgs() << "replacing call with " << newCall << '\n'); |
| 508 |
if (wrap) |
| 509 |
newCallResults.push_back((*wrap)(newCall.getOperation())); |
| 510 |
else |
| 511 |
newCallResults.append(newCall.result_begin(), newCall.result_end()); |
| 512 |
} else { |
| 513 |
fir::DispatchOp dispatchOp = rewriter->create<A>( |
| 514 |
loc, newResTys, rewriter->getStringAttr(callOp.getMethod()), |
| 515 |
callOp.getOperands()[0], newOpers, |
| 516 |
rewriter->getI32IntegerAttr(*callOp.getPassArgPos() + passArgShift)); |
| 517 |
if (wrap) |
| 518 |
newCallResults.push_back((*wrap)(dispatchOp.getOperation())); |
| 519 |
else |
| 520 |
newCallResults.append(dispatchOp.result_begin(), |
| 521 |
dispatchOp.result_end()); |
| 522 |
} |
| 523 |
|
| 524 |
if (newCallResults.size() <= 1) { |
| 525 |
if (savedStackPtr) { |
| 526 |
if (newCallResults.size() == 1) { |
| 527 |
// We assume that all the allocas are inserted before |
| 528 |
// the operation that defines the new call result. |
| 529 |
rewriter->setInsertionPointAfterValue(newCallResults[0]); |
| 530 |
} else { |
| 531 |
// If the call does not have results, then insert |
| 532 |
// stack restore after the original call operation. |
| 533 |
rewriter->setInsertionPointAfter(callOp); |
| 534 |
} |
| 535 |
genStackRestore(loc, savedStackPtr); |
| 536 |
} |
| 537 |
replaceOp(callOp, newCallResults); |
| 538 |
} else { |
| 539 |
// The TODO is duplicated here to make sure this part |
| 540 |
// handles the stackrestore insertion properly, if |
| 541 |
// we add support for multiple call results. |
| 542 |
TODO(loc, "multiple results not supported yet"); |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
// Result type fixup for fir::ComplexType and mlir::ComplexType |
| 547 |
template <typename A, typename B> |
| 548 |
void lowerComplexSignatureRes( |
| 549 |
mlir::Location loc, A cmplx, B &newResTys, |
| 550 |
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs) { |
| 551 |
if (noComplexConversion) { |
| 552 |
newResTys.push_back(cmplx); |
| 553 |
return; |
| 554 |
} |
| 555 |
for (auto &tup : |
| 556 |
specifics->complexReturnType(loc, cmplx.getElementType())) { |
| 557 |
auto argTy = std::get<mlir::Type>(tup); |
| 558 |
if (std::get<fir::CodeGenSpecifics::Attributes>(tup).isSRet()) |
| 559 |
newInTyAndAttrs.push_back(tup); |
| 560 |
else |
| 561 |
newResTys.push_back(argTy); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
// Argument type fixup for fir::ComplexType and mlir::ComplexType |
| 566 |
template <typename A> |
| 567 |
void lowerComplexSignatureArg( |
| 568 |
mlir::Location loc, A cmplx, |
| 569 |
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs) { |
| 570 |
if (noComplexConversion) { |
| 571 |
newInTyAndAttrs.push_back(fir::CodeGenSpecifics::getTypeAndAttr(cmplx)); |
| 572 |
} else { |
| 573 |
auto cplxArgs = |
| 574 |
specifics->complexArgumentType(loc, cmplx.getElementType()); |
| 575 |
newInTyAndAttrs.insert(newInTyAndAttrs.end(), cplxArgs.begin(), |
| 576 |
cplxArgs.end()); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
void |
| 581 |
lowerStructSignatureArg(mlir::Location loc, fir::RecordType recTy, |
| 582 |
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs) { |
| 583 |
if (noStructConversion) { |
| 584 |
newInTyAndAttrs.push_back(fir::CodeGenSpecifics::getTypeAndAttr(recTy)); |
| 585 |
return; |
| 586 |
} |
| 587 |
auto structArgs = |
| 588 |
specifics->structArgumentType(loc, recTy, newInTyAndAttrs); |
| 589 |
newInTyAndAttrs.insert(newInTyAndAttrs.end(), structArgs.begin(), |
| 590 |
structArgs.end()); |
| 591 |
} |
| 592 |
|
| 593 |
llvm::SmallVector<mlir::Type> |
| 594 |
toTypeList(const fir::CodeGenSpecifics::Marshalling &marshalled) { |
| 595 |
llvm::SmallVector<mlir::Type> typeList; |
| 596 |
for (auto &typeAndAttr : marshalled) |
| 597 |
typeList.emplace_back(std::get<mlir::Type>(typeAndAttr)); |
| 598 |
return typeList; |
| 599 |
} |
| 600 |
|
| 601 |
/// Taking the address of a function. Modify the signature as needed. |
| 602 |
void convertAddrOp(fir::AddrOfOp addrOp) { |
| 603 |
rewriter->setInsertionPoint(addrOp); |
| 604 |
auto addrTy = mlir::cast<mlir::FunctionType>(addrOp.getType()); |
| 605 |
fir::CodeGenSpecifics::Marshalling newInTyAndAttrs; |
| 606 |
llvm::SmallVector<mlir::Type> newResTys; |
| 607 |
auto loc = addrOp.getLoc(); |
| 608 |
for (mlir::Type ty : addrTy.getResults()) { |
| 609 |
llvm::TypeSwitch<mlir::Type>(ty) |
| 610 |
.Case<fir::ComplexType>([&](fir::ComplexType ty) { |
| 611 |
lowerComplexSignatureRes(loc, ty, newResTys, newInTyAndAttrs); |
| 612 |
}) |
| 613 |
.Case<mlir::ComplexType>([&](mlir::ComplexType ty) { |
| 614 |
lowerComplexSignatureRes(loc, ty, newResTys, newInTyAndAttrs); |
| 615 |
}) |
| 616 |
.Default([&](mlir::Type ty) { newResTys.push_back(ty); }); |
| 617 |
} |
| 618 |
llvm::SmallVector<mlir::Type> trailingInTys; |
| 619 |
for (mlir::Type ty : addrTy.getInputs()) { |
| 620 |
llvm::TypeSwitch<mlir::Type>(ty) |
| 621 |
.Case<fir::BoxCharType>([&](auto box) { |
| 622 |
if (noCharacterConversion) { |
| 623 |
newInTyAndAttrs.push_back( |
| 624 |
fir::CodeGenSpecifics::getTypeAndAttr(box)); |
| 625 |
} else { |
| 626 |
for (auto &tup : specifics->boxcharArgumentType(box.getEleTy())) { |
| 627 |
auto attr = std::get<fir::CodeGenSpecifics::Attributes>(tup); |
| 628 |
auto argTy = std::get<mlir::Type>(tup); |
| 629 |
if (attr.isAppend()) |
| 630 |
trailingInTys.push_back(argTy); |
| 631 |
else |
| 632 |
newInTyAndAttrs.push_back(tup); |
| 633 |
} |
| 634 |
} |
| 635 |
}) |
| 636 |
.Case<fir::ComplexType>([&](fir::ComplexType ty) { |
| 637 |
lowerComplexSignatureArg(loc, ty, newInTyAndAttrs); |
| 638 |
}) |
| 639 |
.Case<mlir::ComplexType>([&](mlir::ComplexType ty) { |
| 640 |
lowerComplexSignatureArg(loc, ty, newInTyAndAttrs); |
| 641 |
}) |
| 642 |
.Case<mlir::TupleType>([&](mlir::TupleType tuple) { |
| 643 |
if (fir::isCharacterProcedureTuple(tuple)) { |
| 644 |
newInTyAndAttrs.push_back( |
| 645 |
fir::CodeGenSpecifics::getTypeAndAttr(tuple.getType(0))); |
| 646 |
trailingInTys.push_back(tuple.getType(1)); |
| 647 |
} else { |
| 648 |
newInTyAndAttrs.push_back( |
| 649 |
fir::CodeGenSpecifics::getTypeAndAttr(ty)); |
| 650 |
} |
| 651 |
}) |
| 652 |
.template Case<fir::RecordType>([&](fir::RecordType recTy) { |
| 653 |
lowerStructSignatureArg(loc, recTy, newInTyAndAttrs); |
| 654 |
}) |
| 655 |
.Default([&](mlir::Type ty) { |
| 656 |
newInTyAndAttrs.push_back( |
| 657 |
fir::CodeGenSpecifics::getTypeAndAttr(ty)); |
| 658 |
}); |
| 659 |
} |
| 660 |
llvm::SmallVector<mlir::Type> newInTypes = toTypeList(newInTyAndAttrs); |
| 661 |
// append trailing input types |
| 662 |
newInTypes.insert(newInTypes.end(), trailingInTys.begin(), |
| 663 |
trailingInTys.end()); |
| 664 |
// replace this op with a new one with the updated signature |
| 665 |
auto newTy = rewriter->getFunctionType(newInTypes, newResTys); |
| 666 |
auto newOp = rewriter->create<fir::AddrOfOp>(addrOp.getLoc(), newTy, |
| 667 |
addrOp.getSymbol()); |
| 668 |
replaceOp(addrOp, newOp.getResult()); |
| 669 |
} |
| 670 |
|
| 671 |
/// Convert the type signatures on all the functions present in the module. |
| 672 |
/// As the type signature is being changed, this must also update the |
| 673 |
/// function itself to use any new arguments, etc. |
| 674 |
llvm::LogicalResult convertTypes(mlir::ModuleOp mod) { |
| 675 |
mlir::MLIRContext *ctx = mod->getContext(); |
| 676 |
auto targetCPU = specifics->getTargetCPU(); |
| 677 |
mlir::StringAttr targetCPUAttr = |
| 678 |
targetCPU.empty() ? nullptr : mlir::StringAttr::get(ctx, targetCPU); |
| 679 |
auto tuneCPU = specifics->getTuneCPU(); |
| 680 |
mlir::StringAttr tuneCPUAttr = |
| 681 |
tuneCPU.empty() ? nullptr : mlir::StringAttr::get(ctx, tuneCPU); |
| 682 |
auto targetFeaturesAttr = specifics->getTargetFeatures(); |
| 683 |
|
| 684 |
for (auto fn : mod.getOps<mlir::func::FuncOp>()) { |
| 685 |
if (targetCPUAttr) |
| 686 |
fn->setAttr("target_cpu", targetCPUAttr); |
| 687 |
|
| 688 |
if (tuneCPUAttr) |
| 689 |
fn->setAttr("tune_cpu", tuneCPUAttr); |
| 690 |
|
| 691 |
if (targetFeaturesAttr) |
| 692 |
fn->setAttr("target_features", targetFeaturesAttr); |
| 693 |
|
| 694 |
convertSignature(fn); |
| 695 |
} |
| 696 |
return mlir::success(); |
| 697 |
} |
| 698 |
|
| 699 |
// Returns true if the function should be interoperable with C. |
| 700 |
static bool isFuncWithCCallingConvention(mlir::Operation *op) { |
| 701 |
auto funcOp = mlir::dyn_cast<mlir::func::FuncOp>(op); |
| 702 |
if (!funcOp) |
| 703 |
return false; |
| 704 |
return op->hasAttrOfType<mlir::UnitAttr>( |
| 705 |
fir::FIROpsDialect::getFirRuntimeAttrName()) || |
| 706 |
op->hasAttrOfType<mlir::StringAttr>(fir::getSymbolAttrName()); |
| 707 |
} |
| 708 |
|
| 709 |
/// If the signature does not need any special target-specific conversions, |
| 710 |
/// then it is considered portable for any target, and this function will |
| 711 |
/// return `true`. Otherwise, the signature is not portable and `false` is |
| 712 |
/// returned. |
| 713 |
bool hasPortableSignature(mlir::Type signature, mlir::Operation *op) { |
| 714 |
assert(mlir::isa<mlir::FunctionType>(signature)); |
| 715 |
auto func = mlir::dyn_cast<mlir::FunctionType>(signature); |
| 716 |
bool hasCCallingConv = isFuncWithCCallingConvention(op); |
| 717 |
for (auto ty : func.getResults()) |
| 718 |
if ((mlir::isa<fir::BoxCharType>(ty) && !noCharacterConversion) || |
| 719 |
(fir::isa_complex(ty) && !noComplexConversion) || |
| 720 |
(mlir::isa<mlir::IntegerType>(ty) && hasCCallingConv)) { |
| 721 |
LLVM_DEBUG(llvm::dbgs() << "rewrite " << signature << " for target\n"); |
| 722 |
return false; |
| 723 |
} |
| 724 |
for (auto ty : func.getInputs()) |
| 725 |
if (((mlir::isa<fir::BoxCharType>(ty) || |
| 726 |
fir::isCharacterProcedureTuple(ty)) && |
| 727 |
!noCharacterConversion) || |
| 728 |
(fir::isa_complex(ty) && !noComplexConversion) || |
| 729 |
(mlir::isa<mlir::IntegerType>(ty) && hasCCallingConv) || |
| 730 |
(mlir::isa<fir::RecordType>(ty) && !noStructConversion)) { |
| 731 |
LLVM_DEBUG(llvm::dbgs() << "rewrite " << signature << " for target\n"); |
| 732 |
return false; |
| 733 |
} |
| 734 |
return true; |
| 735 |
} |
| 736 |
|
| 737 |
/// Determine if the signature has host associations. The host association |
| 738 |
/// argument may need special target specific rewriting. |
| 739 |
static bool hasHostAssociations(mlir::func::FuncOp func) { |
| 740 |
std::size_t end = func.getFunctionType().getInputs().size(); |
| 741 |
for (std::size_t i = 0; i < end; ++i) |
| 742 |
if (func.getArgAttrOfType<mlir::UnitAttr>(i, fir::getHostAssocAttrName())) |
| 743 |
return true; |
| 744 |
return false; |
| 745 |
} |
| 746 |
|
| 747 |
/// Rewrite the signatures and body of the `FuncOp`s in the module for |
| 748 |
/// the immediately subsequent target code gen. |
| 749 |
void convertSignature(mlir::func::FuncOp func) { |
| 750 |
auto funcTy = mlir::cast<mlir::FunctionType>(func.getFunctionType()); |
| 751 |
if (hasPortableSignature(funcTy, func) && !hasHostAssociations(func)) |
| 752 |
return; |
| 753 |
llvm::SmallVector<mlir::Type> newResTys; |
| 754 |
fir::CodeGenSpecifics::Marshalling newInTyAndAttrs; |
| 755 |
llvm::SmallVector<std::pair<unsigned, mlir::NamedAttribute>> savedAttrs; |
| 756 |
llvm::SmallVector<std::pair<unsigned, mlir::NamedAttribute>> extraAttrs; |
| 757 |
llvm::SmallVector<FixupTy> fixups; |
| 758 |
llvm::SmallVector<std::pair<unsigned, mlir::NamedAttrList>, 1> resultAttrs; |
| 759 |
|
| 760 |
// Save argument attributes in case there is a shift so we can replace them |
| 761 |
// correctly. |
| 762 |
for (auto e : llvm::enumerate(funcTy.getInputs())) { |
| 763 |
unsigned index = e.index(); |
| 764 |
llvm::ArrayRef<mlir::NamedAttribute> attrs = |
| 765 |
mlir::function_interface_impl::getArgAttrs(func, index); |
| 766 |
for (mlir::NamedAttribute attr : attrs) { |
| 767 |
savedAttrs.push_back({index, attr}); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
// Convert return value(s) |
| 772 |
for (auto ty : funcTy.getResults()) |
| 773 |
llvm::TypeSwitch<mlir::Type>(ty) |
| 774 |
.Case<fir::ComplexType>([&](fir::ComplexType cmplx) { |
| 775 |
if (noComplexConversion) |
| 776 |
newResTys.push_back(cmplx); |
| 777 |
else |
| 778 |
doComplexReturn(func, cmplx, newResTys, newInTyAndAttrs, fixups); |
| 779 |
}) |
| 780 |
.Case<mlir::ComplexType>([&](mlir::ComplexType cmplx) { |
| 781 |
if (noComplexConversion) |
| 782 |
newResTys.push_back(cmplx); |
| 783 |
else |
| 784 |
doComplexReturn(func, cmplx, newResTys, newInTyAndAttrs, fixups); |
| 785 |
}) |
| 786 |
.Case<mlir::IntegerType>([&](mlir::IntegerType intTy) { |
| 787 |
auto m = specifics->integerArgumentType(func.getLoc(), intTy); |
| 788 |
assert(m.size() == 1); |
| 789 |
auto attr = std::get<fir::CodeGenSpecifics::Attributes>(m[0]); |
| 790 |
auto retTy = std::get<mlir::Type>(m[0]); |
| 791 |
std::size_t resId = newResTys.size(); |
| 792 |
llvm::StringRef extensionAttrName = attr.getIntExtensionAttrName(); |
| 793 |
if (!extensionAttrName.empty() && |
| 794 |
isFuncWithCCallingConvention(func)) |
| 795 |
resultAttrs.emplace_back( |
| 796 |
resId, rewriter->getNamedAttr(extensionAttrName, |
| 797 |
rewriter->getUnitAttr())); |
| 798 |
newResTys.push_back(retTy); |
| 799 |
}) |
| 800 |
.Default([&](mlir::Type ty) { newResTys.push_back(ty); }); |
| 801 |
|
| 802 |
// Saved potential shift in argument. Handling of result can add arguments |
| 803 |
// at the beginning of the function signature. |
| 804 |
unsigned argumentShift = newInTyAndAttrs.size(); |
| 805 |
|
| 806 |
// Convert arguments |
| 807 |
llvm::SmallVector<mlir::Type> trailingTys; |
| 808 |
for (auto e : llvm::enumerate(funcTy.getInputs())) { |
| 809 |
auto ty = e.value(); |
| 810 |
unsigned index = e.index(); |
| 811 |
llvm::TypeSwitch<mlir::Type>(ty) |
| 812 |
.Case<fir::BoxCharType>([&](fir::BoxCharType boxTy) { |
| 813 |
if (noCharacterConversion) { |
| 814 |
newInTyAndAttrs.push_back( |
| 815 |
fir::CodeGenSpecifics::getTypeAndAttr(boxTy)); |
| 816 |
} else { |
| 817 |
// Convert a CHARACTER argument type. This can involve separating |
| 818 |
// the pointer and the LEN into two arguments and moving the LEN |
| 819 |
// argument to the end of the arg list. |
| 820 |
bool sret = functionArgIsSRet(index, func); |
| 821 |
for (auto e : llvm::enumerate(specifics->boxcharArgumentType( |
| 822 |
boxTy.getEleTy(), sret))) { |
| 823 |
auto &tup = e.value(); |
| 824 |
auto index = e.index(); |
| 825 |
auto attr = std::get<fir::CodeGenSpecifics::Attributes>(tup); |
| 826 |
auto argTy = std::get<mlir::Type>(tup); |
| 827 |
if (attr.isAppend()) { |
| 828 |
trailingTys.push_back(argTy); |
| 829 |
} else { |
| 830 |
if (sret) { |
| 831 |
fixups.emplace_back(FixupTy::Codes::CharPair, |
| 832 |
newInTyAndAttrs.size(), index); |
| 833 |
} else { |
| 834 |
fixups.emplace_back(FixupTy::Codes::Trailing, |
| 835 |
newInTyAndAttrs.size(), |
| 836 |
trailingTys.size()); |
| 837 |
} |
| 838 |
newInTyAndAttrs.push_back(tup); |
| 839 |
} |
| 840 |
} |
| 841 |
} |
| 842 |
}) |
| 843 |
.Case<fir::ComplexType>([&](fir::ComplexType cmplx) { |
| 844 |
doComplexArg(func, cmplx, newInTyAndAttrs, fixups); |
| 845 |
}) |
| 846 |
.Case<mlir::ComplexType>([&](mlir::ComplexType cmplx) { |
| 847 |
doComplexArg(func, cmplx, newInTyAndAttrs, fixups); |
| 848 |
}) |
| 849 |
.Case<mlir::TupleType>([&](mlir::TupleType tuple) { |
| 850 |
if (fir::isCharacterProcedureTuple(tuple)) { |
| 851 |
fixups.emplace_back(FixupTy::Codes::TrailingCharProc, |
| 852 |
newInTyAndAttrs.size(), trailingTys.size()); |
| 853 |
newInTyAndAttrs.push_back( |
| 854 |
fir::CodeGenSpecifics::getTypeAndAttr(tuple.getType(0))); |
| 855 |
trailingTys.push_back(tuple.getType(1)); |
| 856 |
} else { |
| 857 |
newInTyAndAttrs.push_back( |
| 858 |
fir::CodeGenSpecifics::getTypeAndAttr(ty)); |
| 859 |
} |
| 860 |
}) |
| 861 |
.Case<mlir::IntegerType>([&](mlir::IntegerType intTy) { |
| 862 |
auto m = specifics->integerArgumentType(func.getLoc(), intTy); |
| 863 |
assert(m.size() == 1); |
| 864 |
auto attr = std::get<fir::CodeGenSpecifics::Attributes>(m[0]); |
| 865 |
auto argNo = newInTyAndAttrs.size(); |
| 866 |
llvm::StringRef extensionAttrName = attr.getIntExtensionAttrName(); |
| 867 |
if (!extensionAttrName.empty() && |
| 868 |
isFuncWithCCallingConvention(func)) |
| 869 |
fixups.emplace_back(FixupTy::Codes::ArgumentType, argNo, |
| 870 |
[=](mlir::func::FuncOp func) { |
| 871 |
func.setArgAttr( |
| 872 |
argNo, extensionAttrName, |
| 873 |
mlir::UnitAttr::get(func.getContext())); |
| 874 |
}); |
| 875 |
|
| 876 |
newInTyAndAttrs.push_back(m[0]); |
| 877 |
}) |
| 878 |
.template Case<fir::RecordType>([&](fir::RecordType recTy) { |
| 879 |
doStructArg(func, recTy, newInTyAndAttrs, fixups); |
| 880 |
}) |
| 881 |
.Default([&](mlir::Type ty) { |
| 882 |
newInTyAndAttrs.push_back( |
| 883 |
fir::CodeGenSpecifics::getTypeAndAttr(ty)); |
| 884 |
}); |
| 885 |
|
| 886 |
if (func.getArgAttrOfType<mlir::UnitAttr>(index, |
| 887 |
fir::getHostAssocAttrName())) { |
| 888 |
extraAttrs.push_back( |
| 889 |
{newInTyAndAttrs.size() - 1, |
| 890 |
rewriter->getNamedAttr("llvm.nest", rewriter->getUnitAttr())}); |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
if (!func.empty()) { |
| 895 |
// If the function has a body, then apply the fixups to the arguments and |
| 896 |
// return ops as required. These fixups are done in place. |
| 897 |
auto loc = func.getLoc(); |
| 898 |
const auto fixupSize = fixups.size(); |
| 899 |
const auto oldArgTys = func.getFunctionType().getInputs(); |
| 900 |
int offset = 0; |
| 901 |
for (std::remove_const_t<decltype(fixupSize)> i = 0; i < fixupSize; ++i) { |
| 902 |
const auto &fixup = fixups[i]; |
| 903 |
mlir::Type fixupType = |
| 904 |
fixup.index < newInTyAndAttrs.size() |
| 905 |
? std::get<mlir::Type>(newInTyAndAttrs[fixup.index]) |
| 906 |
: mlir::Type{}; |
| 907 |
switch (fixup.code) { |
| 908 |
case FixupTy::Codes::ArgumentAsLoad: { |
| 909 |
// Argument was pass-by-value, but is now pass-by-reference and |
| 910 |
// possibly with a different element type. |
| 911 |
auto newArg = |
| 912 |
func.front().insertArgument(fixup.index, fixupType, loc); |
| 913 |
rewriter->setInsertionPointToStart(&func.front()); |
| 914 |
auto oldArgTy = |
| 915 |
fir::ReferenceType::get(oldArgTys[fixup.index - offset]); |
| 916 |
auto cast = rewriter->create<fir::ConvertOp>(loc, oldArgTy, newArg); |
| 917 |
auto load = rewriter->create<fir::LoadOp>(loc, cast); |
| 918 |
func.getArgument(fixup.index + 1).replaceAllUsesWith(load); |
| 919 |
func.front().eraseArgument(fixup.index + 1); |
| 920 |
} break; |
| 921 |
case FixupTy::Codes::ArgumentType: { |
| 922 |
// Argument is pass-by-value, but its type has likely been modified to |
| 923 |
// suit the target ABI convention. |
| 924 |
auto oldArgTy = oldArgTys[fixup.index - offset]; |
| 925 |
// If type did not change, keep the original argument. |
| 926 |
if (fixupType == oldArgTy) |
| 927 |
break; |
| 928 |
|
| 929 |
auto newArg = |
| 930 |
func.front().insertArgument(fixup.index, fixupType, loc); |
| 931 |
rewriter->setInsertionPointToStart(&func.front()); |
| 932 |
mlir::Value bitcast = convertValueInMemory(loc, newArg, oldArgTy, |
| 933 |
/*inputMayBeBigger=*/true); |
| 934 |
func.getArgument(fixup.index + 1).replaceAllUsesWith(bitcast); |
| 935 |
func.front().eraseArgument(fixup.index + 1); |
| 936 |
LLVM_DEBUG(llvm::dbgs() |
| 937 |
<< "old argument: " << oldArgTy << ", repl: " << bitcast |
| 938 |
<< ", new argument: " |
| 939 |
<< func.getArgument(fixup.index).getType() << '\n'); |
| 940 |
} break; |
| 941 |
case FixupTy::Codes::CharPair: { |
| 942 |
// The FIR boxchar argument has been split into a pair of distinct |
| 943 |
// arguments that are in juxtaposition to each other. |
| 944 |
auto newArg = |
| 945 |
func.front().insertArgument(fixup.index, fixupType, loc); |
| 946 |
if (fixup.second == 1) { |
| 947 |
rewriter->setInsertionPointToStart(&func.front()); |
| 948 |
auto boxTy = oldArgTys[fixup.index - offset - fixup.second]; |
| 949 |
auto box = rewriter->create<fir::EmboxCharOp>( |
| 950 |
loc, boxTy, func.front().getArgument(fixup.index - 1), newArg); |
| 951 |
func.getArgument(fixup.index + 1).replaceAllUsesWith(box); |
| 952 |
func.front().eraseArgument(fixup.index + 1); |
| 953 |
offset++; |
| 954 |
} |
| 955 |
} break; |
| 956 |
case FixupTy::Codes::ReturnAsStore: { |
| 957 |
// The value being returned is now being returned in memory (callee |
| 958 |
// stack space) through a hidden reference argument. |
| 959 |
auto newArg = |
| 960 |
func.front().insertArgument(fixup.index, fixupType, loc); |
| 961 |
offset++; |
| 962 |
func.walk([&](mlir::func::ReturnOp ret) { |
| 963 |
rewriter->setInsertionPoint(ret); |
| 964 |
auto oldOper = ret.getOperand(0); |
| 965 |
auto oldOperTy = fir::ReferenceType::get(oldOper.getType()); |
| 966 |
auto cast = |
| 967 |
rewriter->create<fir::ConvertOp>(loc, oldOperTy, newArg); |
| 968 |
rewriter->create<fir::StoreOp>(loc, oldOper, cast); |
| 969 |
rewriter->create<mlir::func::ReturnOp>(loc); |
| 970 |
ret.erase(); |
| 971 |
}); |
| 972 |
} break; |
| 973 |
case FixupTy::Codes::ReturnType: { |
| 974 |
// The function is still returning a value, but its type has likely |
| 975 |
// changed to suit the target ABI convention. |
| 976 |
func.walk([&](mlir::func::ReturnOp ret) { |
| 977 |
rewriter->setInsertionPoint(ret); |
| 978 |
auto oldOper = ret.getOperand(0); |
| 979 |
mlir::Value bitcast = |
| 980 |
convertValueInMemory(loc, oldOper, newResTys[fixup.index], |
| 981 |
/*inputMayBeBigger=*/false); |
| 982 |
rewriter->create<mlir::func::ReturnOp>(loc, bitcast); |
| 983 |
ret.erase(); |
| 984 |
}); |
| 985 |
} break; |
| 986 |
case FixupTy::Codes::Split: { |
| 987 |
// The FIR argument has been split into a pair of distinct arguments |
| 988 |
// that are in juxtaposition to each other. (For COMPLEX value or |
| 989 |
// derived type passed with VALUE in BIND(C) context). |
| 990 |
auto newArg = |
| 991 |
func.front().insertArgument(fixup.index, fixupType, loc); |
| 992 |
if (fixup.second == 1) { |
| 993 |
rewriter->setInsertionPointToStart(&func.front()); |
| 994 |
mlir::Value firstArg = func.front().getArgument(fixup.index - 1); |
| 995 |
mlir::Type originalTy = |
| 996 |
oldArgTys[fixup.index - offset - fixup.second]; |
| 997 |
mlir::Type pairTy = originalTy; |
| 998 |
if (!fir::isa_complex(originalTy)) { |
| 999 |
pairTy = mlir::TupleType::get( |
| 1000 |
originalTy.getContext(), |
| 1001 |
mlir::TypeRange{firstArg.getType(), newArg.getType()}); |
| 1002 |
} |
| 1003 |
auto undef = rewriter->create<fir::UndefOp>(loc, pairTy); |
| 1004 |
auto iTy = rewriter->getIntegerType(32); |
| 1005 |
auto zero = rewriter->getIntegerAttr(iTy, 0); |
| 1006 |
auto one = rewriter->getIntegerAttr(iTy, 1); |
| 1007 |
mlir::Value pair1 = rewriter->create<fir::InsertValueOp>( |
| 1008 |
loc, pairTy, undef, firstArg, rewriter->getArrayAttr(zero)); |
| 1009 |
mlir::Value pair = rewriter->create<fir::InsertValueOp>( |
| 1010 |
loc, pairTy, pair1, newArg, rewriter->getArrayAttr(one)); |
| 1011 |
// Cast local argument tuple to original type via memory if needed. |
| 1012 |
if (pairTy != originalTy) |
| 1013 |
pair = convertValueInMemory(loc, pair, originalTy, |
| 1014 |
/*inputMayBeBigger=*/true); |
| 1015 |
func.getArgument(fixup.index + 1).replaceAllUsesWith(pair); |
| 1016 |
func.front().eraseArgument(fixup.index + 1); |
| 1017 |
offset++; |
| 1018 |
} |
| 1019 |
} break; |
| 1020 |
case FixupTy::Codes::Trailing: { |
| 1021 |
// The FIR argument has been split into a pair of distinct arguments. |
| 1022 |
// The first part of the pair appears in the original argument |
| 1023 |
// position. The second part of the pair is appended after all the |
| 1024 |
// original arguments. (Boxchar arguments.) |
| 1025 |
auto newBufArg = |
| 1026 |
func.front().insertArgument(fixup.index, fixupType, loc); |
| 1027 |
auto newLenArg = |
| 1028 |
func.front().addArgument(trailingTys[fixup.second], loc); |
| 1029 |
auto boxTy = oldArgTys[fixup.index - offset]; |
| 1030 |
rewriter->setInsertionPointToStart(&func.front()); |
| 1031 |
auto box = rewriter->create<fir::EmboxCharOp>(loc, boxTy, newBufArg, |
| 1032 |
newLenArg); |
| 1033 |
func.getArgument(fixup.index + 1).replaceAllUsesWith(box); |
| 1034 |
func.front().eraseArgument(fixup.index + 1); |
| 1035 |
} break; |
| 1036 |
case FixupTy::Codes::TrailingCharProc: { |
| 1037 |
// The FIR character procedure argument tuple must be split into a |
| 1038 |
// pair of distinct arguments. The first part of the pair appears in |
| 1039 |
// the original argument position. The second part of the pair is |
| 1040 |
// appended after all the original arguments. |
| 1041 |
auto newProcPointerArg = |
| 1042 |
func.front().insertArgument(fixup.index, fixupType, loc); |
| 1043 |
auto newLenArg = |
| 1044 |
func.front().addArgument(trailingTys[fixup.second], loc); |
| 1045 |
auto tupleType = oldArgTys[fixup.index - offset]; |
| 1046 |
rewriter->setInsertionPointToStart(&func.front()); |
| 1047 |
fir::FirOpBuilder builder(*rewriter, getModule()); |
| 1048 |
auto tuple = fir::factory::createCharacterProcedureTuple( |
| 1049 |
builder, loc, tupleType, newProcPointerArg, newLenArg); |
| 1050 |
func.getArgument(fixup.index + 1).replaceAllUsesWith(tuple); |
| 1051 |
func.front().eraseArgument(fixup.index + 1); |
| 1052 |
} break; |
| 1053 |
} |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
llvm::SmallVector<mlir::Type> newInTypes = toTypeList(newInTyAndAttrs); |
| 1058 |
// Set the new type and finalize the arguments, etc. |
| 1059 |
newInTypes.insert(newInTypes.end(), trailingTys.begin(), trailingTys.end()); |
| 1060 |
auto newFuncTy = |
| 1061 |
mlir::FunctionType::get(func.getContext(), newInTypes, newResTys); |
| 1062 |
LLVM_DEBUG(llvm::dbgs() << "new func: " << newFuncTy << '\n'); |
| 1063 |
func.setType(newFuncTy); |
| 1064 |
|
| 1065 |
for (std::pair<unsigned, mlir::NamedAttribute> extraAttr : extraAttrs) |
| 1066 |
func.setArgAttr(extraAttr.first, extraAttr.second.getName(), |
| 1067 |
extraAttr.second.getValue()); |
| 1068 |
|
| 1069 |
for (auto [resId, resAttrList] : resultAttrs) |
| 1070 |
for (mlir::NamedAttribute resAttr : resAttrList) |
| 1071 |
func.setResultAttr(resId, resAttr.getName(), resAttr.getValue()); |
| 1072 |
|
| 1073 |
// Replace attributes to the correct argument if there was an argument shift |
| 1074 |
// to the right. |
| 1075 |
if (argumentShift > 0) { |
| 1076 |
for (std::pair<unsigned, mlir::NamedAttribute> savedAttr : savedAttrs) { |
| 1077 |
func.removeArgAttr(savedAttr.first, savedAttr.second.getName()); |
| 1078 |
func.setArgAttr(savedAttr.first + argumentShift, |
| 1079 |
savedAttr.second.getName(), |
| 1080 |
savedAttr.second.getValue()); |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
for (auto &fixup : fixups) |
| 1085 |
if (fixup.finalizer) |
| 1086 |
(*fixup.finalizer)(func); |
| 1087 |
} |
| 1088 |
|
| 1089 |
inline bool functionArgIsSRet(unsigned index, mlir::func::FuncOp func) { |
| 1090 |
if (auto attr = func.getArgAttrOfType<mlir::TypeAttr>(index, "llvm.sret")) |
| 1091 |
return true; |
| 1092 |
return false; |
| 1093 |
} |
| 1094 |
|
| 1095 |
/// Convert a complex return value. This can involve converting the return |
| 1096 |
/// value to a "hidden" first argument or packing the complex into a wide |
| 1097 |
/// GPR. |
| 1098 |
template <typename A, typename B, typename C> |
| 1099 |
void doComplexReturn(mlir::func::FuncOp func, A cmplx, B &newResTys, |
| 1100 |
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs, |
| 1101 |
C &fixups) { |
| 1102 |
if (noComplexConversion) { |
| 1103 |
newResTys.push_back(cmplx); |
| 1104 |
return; |
| 1105 |
} |
| 1106 |
auto m = |
| 1107 |
specifics->complexReturnType(func.getLoc(), cmplx.getElementType()); |
| 1108 |
assert(m.size() == 1); |
| 1109 |
auto &tup = m[0]; |
| 1110 |
auto attr = std::get<fir::CodeGenSpecifics::Attributes>(tup); |
| 1111 |
auto argTy = std::get<mlir::Type>(tup); |
| 1112 |
if (attr.isSRet()) { |
| 1113 |
unsigned argNo = newInTyAndAttrs.size(); |
| 1114 |
if (auto align = attr.getAlignment()) |
| 1115 |
fixups.emplace_back( |
| 1116 |
FixupTy::Codes::ReturnAsStore, argNo, [=](mlir::func::FuncOp func) { |
| 1117 |
auto elemType = fir::dyn_cast_ptrOrBoxEleTy( |
| 1118 |
func.getFunctionType().getInput(argNo)); |
| 1119 |
func.setArgAttr(argNo, "llvm.sret", |
| 1120 |
mlir::TypeAttr::get(elemType)); |
| 1121 |
func.setArgAttr(argNo, "llvm.align", |
| 1122 |
rewriter->getIntegerAttr( |
| 1123 |
rewriter->getIntegerType(32), align)); |
| 1124 |
}); |
| 1125 |
else |
| 1126 |
fixups.emplace_back(FixupTy::Codes::ReturnAsStore, argNo, |
| 1127 |
[=](mlir::func::FuncOp func) { |
| 1128 |
auto elemType = fir::dyn_cast_ptrOrBoxEleTy( |
| 1129 |
func.getFunctionType().getInput(argNo)); |
| 1130 |
func.setArgAttr(argNo, "llvm.sret", |
| 1131 |
mlir::TypeAttr::get(elemType)); |
| 1132 |
}); |
| 1133 |
newInTyAndAttrs.push_back(tup); |
| 1134 |
return; |
| 1135 |
} |
| 1136 |
if (auto align = attr.getAlignment()) |
| 1137 |
fixups.emplace_back( |
| 1138 |
FixupTy::Codes::ReturnType, newResTys.size(), |
| 1139 |
[=](mlir::func::FuncOp func) { |
| 1140 |
func.setArgAttr( |
| 1141 |
newResTys.size(), "llvm.align", |
| 1142 |
rewriter->getIntegerAttr(rewriter->getIntegerType(32), align)); |
| 1143 |
}); |
| 1144 |
else |
| 1145 |
fixups.emplace_back(FixupTy::Codes::ReturnType, newResTys.size()); |
| 1146 |
newResTys.push_back(argTy); |
| 1147 |
} |
| 1148 |
|
| 1149 |
template <typename FIXUPS> |
| 1150 |
void |
| 1151 |
createFuncOpArgFixups(mlir::func::FuncOp func, |
| 1152 |
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs, |
| 1153 |
fir::CodeGenSpecifics::Marshalling &argsInTys, |
| 1154 |
FIXUPS &fixups) { |
| 1155 |
const auto fixupCode = argsInTys.size() > 1 ? FixupTy::Codes::Split |
| 1156 |
: FixupTy::Codes::ArgumentType; |
| 1157 |
for (auto e : llvm::enumerate(argsInTys)) { |
| 1158 |
auto &tup = e.value(); |
| 1159 |
auto index = e.index(); |
| 1160 |
auto attr = std::get<fir::CodeGenSpecifics::Attributes>(tup); |
| 1161 |
auto argNo = newInTyAndAttrs.size(); |
| 1162 |
if (attr.isByVal()) { |
| 1163 |
if (auto align = attr.getAlignment()) |
| 1164 |
fixups.emplace_back(FixupTy::Codes::ArgumentAsLoad, argNo, |
| 1165 |
[=](mlir::func::FuncOp func) { |
| 1166 |
auto elemType = fir::dyn_cast_ptrOrBoxEleTy( |
| 1167 |
func.getFunctionType().getInput(argNo)); |
| 1168 |
func.setArgAttr(argNo, "llvm.byval", |
| 1169 |
mlir::TypeAttr::get(elemType)); |
| 1170 |
func.setArgAttr( |
| 1171 |
argNo, "llvm.align", |
| 1172 |
rewriter->getIntegerAttr( |
| 1173 |
rewriter->getIntegerType(32), align)); |
| 1174 |
}); |
| 1175 |
else |
| 1176 |
fixups.emplace_back(FixupTy::Codes::ArgumentAsLoad, |
| 1177 |
newInTyAndAttrs.size(), |
| 1178 |
[=](mlir::func::FuncOp func) { |
| 1179 |
auto elemType = fir::dyn_cast_ptrOrBoxEleTy( |
| 1180 |
func.getFunctionType().getInput(argNo)); |
| 1181 |
func.setArgAttr(argNo, "llvm.byval", |
| 1182 |
mlir::TypeAttr::get(elemType)); |
| 1183 |
}); |
| 1184 |
} else { |
| 1185 |
if (auto align = attr.getAlignment()) |
| 1186 |
fixups.emplace_back( |
| 1187 |
fixupCode, argNo, index, [=](mlir::func::FuncOp func) { |
| 1188 |
func.setArgAttr(argNo, "llvm.align", |
| 1189 |
rewriter->getIntegerAttr( |
| 1190 |
rewriter->getIntegerType(32), align)); |
| 1191 |
}); |
| 1192 |
else |
| 1193 |
fixups.emplace_back(fixupCode, argNo, index); |
| 1194 |
} |
| 1195 |
newInTyAndAttrs.push_back(tup); |
| 1196 |
} |
| 1197 |
} |
| 1198 |
|
| 1199 |
/// Convert a complex argument value. This can involve storing the value to |
| 1200 |
/// a temporary memory location or factoring the value into two distinct |
| 1201 |
/// arguments. |
| 1202 |
template <typename A, typename B> |
| 1203 |
void doComplexArg(mlir::func::FuncOp func, A cmplx, |
| 1204 |
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs, |
| 1205 |
B &fixups) { |
| 1206 |
if (noComplexConversion) { |
| 1207 |
newInTyAndAttrs.push_back(fir::CodeGenSpecifics::getTypeAndAttr(cmplx)); |
| 1208 |
return; |
| 1209 |
} |
| 1210 |
auto cplxArgs = |
| 1211 |
specifics->complexArgumentType(func.getLoc(), cmplx.getElementType()); |
| 1212 |
createFuncOpArgFixups(func, newInTyAndAttrs, cplxArgs, fixups); |
| 1213 |
} |
| 1214 |
|
| 1215 |
template <typename FIXUPS> |
| 1216 |
void doStructArg(mlir::func::FuncOp func, fir::RecordType recTy, |
| 1217 |
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs, |
| 1218 |
FIXUPS &fixups) { |
| 1219 |
if (noStructConversion) { |
| 1220 |
newInTyAndAttrs.push_back(fir::CodeGenSpecifics::getTypeAndAttr(recTy)); |
| 1221 |
return; |
| 1222 |
} |
| 1223 |
auto structArgs = |
| 1224 |
specifics->structArgumentType(func.getLoc(), recTy, newInTyAndAttrs); |
| 1225 |
createFuncOpArgFixups(func, newInTyAndAttrs, structArgs, fixups); |
| 1226 |
} |
| 1227 |
|
| 1228 |
private: |
| 1229 |
// Replace `op` and remove it. |
| 1230 |
void replaceOp(mlir::Operation *op, mlir::ValueRange newValues) { |
| 1231 |
op->replaceAllUsesWith(newValues); |
| 1232 |
op->dropAllReferences(); |
| 1233 |
op->erase(); |
| 1234 |
} |
| 1235 |
|
| 1236 |
inline void setMembers(fir::CodeGenSpecifics *s, mlir::OpBuilder *r, |
| 1237 |
mlir::DataLayout *dl) { |
| 1238 |
specifics = s; |
| 1239 |
rewriter = r; |
| 1240 |
dataLayout = dl; |
| 1241 |
} |
| 1242 |
|
| 1243 |
inline void clearMembers() { setMembers(nullptr, nullptr, nullptr); } |
| 1244 |
|
| 1245 |
// Inserts a call to llvm.stacksave at the current insertion |
| 1246 |
// point and the given location. Returns the call's result Value. |
| 1247 |
inline mlir::Value genStackSave(mlir::Location loc) { |
| 1248 |
return rewriter->create<fir::CallOp>(loc, stackSaveFn).getResult(0); |
| 1249 |
} |
| 1250 |
|
| 1251 |
// Inserts a call to llvm.stackrestore at the current insertion |
| 1252 |
// point and the given location and argument. |
| 1253 |
inline void genStackRestore(mlir::Location loc, mlir::Value sp) { |
| 1254 |
rewriter->create<fir::CallOp>(loc, stackRestoreFn, mlir::ValueRange{sp}); |
| 1255 |
} |
| 1256 |
|
| 1257 |
fir::CodeGenSpecifics *specifics = nullptr; |
| 1258 |
mlir::OpBuilder *rewriter = nullptr; |
| 1259 |
mlir::DataLayout *dataLayout = nullptr; |
| 1260 |
mlir::func::FuncOp stackSaveFn = nullptr; |
| 1261 |
mlir::func::FuncOp stackRestoreFn = nullptr; |
| 1262 |
}; |
| 1263 |
} // namespace |