Skip to content

[flang] Add option to skip struct argument rewrite in target-rewrite #75939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion flang/include/flang/Optimizer/CodeGen/CGPasses.td
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ def TargetRewritePass : Pass<"target-rewrite", "mlir::ModuleOp"> {
"Disable target-specific conversion of CHARACTER.">,
Option<"noComplexConversion", "no-complex-conversion",
"bool", /*default=*/"false",
"Disable target-specific conversion of COMPLEX.">
"Disable target-specific conversion of COMPLEX.">,
Option<"noStructConversion", "no-struct-conversion",
"bool", /*default=*/"false",
"Disable target-specific conversion of derived type value.">
];
}

Expand Down
1 change: 1 addition & 0 deletions flang/include/flang/Optimizer/CodeGen/CodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ std::unique_ptr<mlir::Pass> createFirCodeGenRewritePass();
struct TargetRewriteOptions {
bool noCharacterConversion{};
bool noComplexConversion{};
bool noStructConversion{};
};

/// Prerequiste pass for code gen. Perform intermediate rewrites to tailor the
Expand Down
16 changes: 15 additions & 1 deletion flang/lib/Optimizer/CodeGen/TargetRewrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
TargetRewrite(const fir::TargetRewriteOptions &options) {
noCharacterConversion = options.noCharacterConversion;
noComplexConversion = options.noComplexConversion;
noStructConversion = options.noStructConversion;
}

void runOnOperation() override final {
Expand Down Expand Up @@ -252,6 +253,11 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs,
llvm::SmallVectorImpl<mlir::Value> &newOpers,
mlir::Value &savedStackPtr) {
if (noStructConversion) {
newInTyAndAttrs.push_back(fir::CodeGenSpecifics::getTypeAndAttr(recTy));
newOpers.push_back(oper);
return;
}
auto structArgs =
specifics->structArgumentType(loc, recTy, newInTyAndAttrs);
if (structArgs.size() != 1)
Expand Down Expand Up @@ -522,6 +528,10 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
void
lowerStructSignatureArg(mlir::Location loc, fir::RecordType recTy,
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs) {
if (noStructConversion) {
newInTyAndAttrs.push_back(fir::CodeGenSpecifics::getTypeAndAttr(recTy));
return;
}
auto structArgs =
specifics->structArgumentType(loc, recTy, newInTyAndAttrs);
newInTyAndAttrs.insert(newInTyAndAttrs.end(), structArgs.begin(),
Expand Down Expand Up @@ -645,7 +655,7 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
!noCharacterConversion) ||
(fir::isa_complex(ty) && !noComplexConversion) ||
(ty.isa<mlir::IntegerType>() && hasCCallingConv) ||
ty.isa<fir::RecordType>()) {
(ty.isa<fir::RecordType>() && !noStructConversion)) {
LLVM_DEBUG(llvm::dbgs() << "rewrite " << signature << " for target\n");
return false;
}
Expand Down Expand Up @@ -1128,6 +1138,10 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
void doStructArg(mlir::func::FuncOp func, fir::RecordType recTy,
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs,
FIXUPS &fixups) {
if (noStructConversion) {
newInTyAndAttrs.push_back(fir::CodeGenSpecifics::getTypeAndAttr(recTy));
return;
}
auto structArgs =
specifics->structArgumentType(func.getLoc(), recTy, newInTyAndAttrs);
createFuncOpArgFixups(func, newInTyAndAttrs, structArgs, fixups);
Expand Down
25 changes: 25 additions & 0 deletions flang/test/Fir/target-rewrite-selective-no-struct.fir
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Test no-struct-conversion of target-rewrite pass.
// RUN: fir-opt -target-rewrite="no-struct-conversion" %s | FileCheck %s

func.func @test(%arg0: !fir.type<t{i:i32}>) {
return
}

func.func @test_call(%arg0: !fir.type<t{i:i32}>) {
fir.call @test(%arg0) : (!fir.type<t{i:i32}>) -> ()
return
}

func.func @test_addr_off() {
%0 = fir.address_of(@test) : (!fir.type<t{i:i32}>) -> ()
return
}

// CHECK-LABEL: func.func @test(%{{.*}}: !fir.type<t{i:i32}>) {

// CHECK-LABEL: func.func @test_call(
// CHECK-SAME: %[[ARG0:.*]]: !fir.type<t{i:i32}>) {
// CHECK: fir.call @test(%[[ARG0]]) : (!fir.type<t{i:i32}>) -> ()

// CHECK-LABEL: func.func @test_addr_off() {
// CHECK: fir.address_of(@test) : (!fir.type<t{i:i32}>) -> ()