// RUN: fir-opt %s --allow-unregistered-dialect --mem2reg --split-input-file | FileCheck %s // CHECK-LABEL: func.func @basic() -> i32 { // CHECK: %[[CONSTANT_0:.*]] = arith.constant 5 : i32 // CHECK: return %[[CONSTANT_0]] : i32 // CHECK: } func.func @basic() -> i32 { %0 = arith.constant 5 : i32 %1 = fir.alloca i32 fir.store %0 to %1 : !fir.ref %2 = fir.load %1 : !fir.ref return %2 : i32 } // ----- // CHECK-LABEL: func.func @default_value() -> i32 { // CHECK: %[[UNDEFINED_0:.*]] = fir.undefined i32 // CHECK: %[[CONSTANT_0:.*]] = arith.constant 5 : i32 // CHECK: return %[[UNDEFINED_0]] : i32 // CHECK: } func.func @default_value() -> i32 { %0 = arith.constant 5 : i32 %1 = fir.alloca i32 %2 = fir.load %1 : !fir.ref fir.store %0 to %1 : !fir.ref return %2 : i32 } // ----- // CHECK-LABEL: func.func @basic_float() -> f32 { // CHECK: %[[CONSTANT_0:.*]] = arith.constant 5.200000e+00 : f32 // CHECK: return %[[CONSTANT_0]] : f32 // CHECK: } func.func @basic_float() -> f32 { %0 = arith.constant 5.2 : f32 %1 = fir.alloca f32 fir.store %0 to %1 : !fir.ref %2 = fir.load %1 : !fir.ref return %2 : f32 } // ----- // CHECK-LABEL: func.func @cycle( // CHECK-SAME: %[[ARG0:.*]]: i64, // CHECK-SAME: %[[ARG1:.*]]: i1, // CHECK-SAME: %[[ARG2:.*]]: i64) { // CHECK: cf.cond_br %[[ARG1]], ^bb1(%[[ARG2]] : i64), ^bb2(%[[ARG2]] : i64) // CHECK: ^bb1(%[[VAL_0:.*]]: i64): // CHECK: "test.use"(%[[VAL_0]]) : (i64) -> () // CHECK: cf.br ^bb2(%[[ARG0]] : i64) // CHECK: ^bb2(%[[VAL_1:.*]]: i64): // CHECK: cf.br ^bb1(%[[VAL_1]] : i64) // CHECK: } func.func @cycle(%arg0: i64, %arg1: i1, %arg2: i64) { %alloca = fir.alloca i64 fir.store %arg2 to %alloca : !fir.ref cf.cond_br %arg1, ^bb1, ^bb2 ^bb1: %use = fir.load %alloca : !fir.ref "test.use"(%use) : (i64) -> () fir.store %arg0 to %alloca : !fir.ref cf.br ^bb2 ^bb2: cf.br ^bb1 } // ----- // CHECK-LABEL: func.func @test_simple_declare(%arg0: !fir.ref {fir.bindc_name = "i"}) { // CHECK: %[[C42:.*]] = arith.constant 42 : i32 // CHECK: %[[SCOPE:.*]] = fir.dummy_scope : !fir.dscope // CHECK: %[[ARG_DECL:.*]] = fir.declare %arg0 dummy_scope %[[SCOPE]] arg 1 {uniq_name = "_QFfooEi"} : (!fir.ref, !fir.dscope) -> !fir.ref // CHECK: fir.declare_value %[[C42]] {uniq_name = "_QFfooEj"} : i32 // CHECK: fir.store %[[C42]] to %[[ARG_DECL]] : !fir.ref func.func @test_simple_declare(%arg0: !fir.ref {fir.bindc_name = "i"}) { %c42_i32 = arith.constant 42 : i32 %0 = fir.dummy_scope : !fir.dscope %1 = fir.declare %arg0 dummy_scope %0 arg 1 {uniq_name = "_QFfooEi"} : (!fir.ref, !fir.dscope) -> !fir.ref %2 = fir.alloca i32 {bindc_name = "j", uniq_name = "_QFfooEj"} %3 = fir.declare %2 {uniq_name = "_QFfooEj"} : (!fir.ref) -> !fir.ref fir.store %c42_i32 to %3 : !fir.ref %4 = fir.load %3 : !fir.ref fir.store %4 to %1 : !fir.ref return } // ----- // CHECK-LABEL: func.func @test_two_values( // CHECK: %[[CONSTANT_0:.*]] = arith.constant 43 : i32 // CHECK: %[[CONSTANT_1:.*]] = arith.constant 42 : i32 // CHECK: fir.declare_value %[[CONSTANT_1]] {uniq_name = "_QFfooEjlocal"} : i32 // CHECK: fir.store %[[CONSTANT_1]] to %{{.*}} : !fir.ref // CHECK: fir.declare_value %[[CONSTANT_0]] {uniq_name = "_QFfooEjlocal"} : i32 // CHECK: fir.store %[[CONSTANT_0]] to %{{.*}} : !fir.ref func.func @test_two_values(%arg0: !fir.ref {fir.bindc_name = "i"}, %arg1: !fir.ref {fir.bindc_name = "j"}) { %c43_i32 = arith.constant 43 : i32 %c42_i32 = arith.constant 42 : i32 %0 = fir.dummy_scope : !fir.dscope %1 = fir.declare %arg0 dummy_scope %0 arg 1 {uniq_name = "_QFfooEi"} : (!fir.ref, !fir.dscope) -> !fir.ref %2 = fir.declare %arg1 dummy_scope %0 arg 2 {uniq_name = "_QFfooEj"} : (!fir.ref, !fir.dscope) -> !fir.ref %3 = fir.alloca i32 {bindc_name = "jlocal", uniq_name = "_QFfooEjlocal"} %4 = fir.declare %3 {uniq_name = "_QFfooEjlocal"} : (!fir.ref) -> !fir.ref fir.store %c42_i32 to %4 : !fir.ref %5 = fir.load %4 : !fir.ref fir.store %5 to %1 : !fir.ref fir.store %c43_i32 to %4 : !fir.ref %6 = fir.load %4 : !fir.ref fir.store %6 to %2 : !fir.ref return } // ----- // CHECK-LABEL: func.func @array_val_not_mem2reg( // CHECK: fir.alloca !fir.array<2xi32> // CHECK: fir.store // CHECK: fir.load // CHECK: fir.store func.func @array_val_not_mem2reg(%arg0: !fir.ref> {fir.bindc_name = "i"}, %arg1: !fir.ref {fir.bindc_name = "j"}, %arrayval : !fir.array<2xi32>) { %c2 = arith.constant 2 : index %0 = fir.dummy_scope : !fir.dscope %1 = fir.shape %c2 : (index) -> !fir.shape<1> %2 = fir.declare %arg0(%1) dummy_scope %0 arg 1 {uniq_name = "_QFarrayEi"} : (!fir.ref>, !fir.shape<1>, !fir.dscope) -> !fir.ref> %3 = fir.declare %arg1 dummy_scope %0 arg 2 {uniq_name = "_QFarrayEj"} : (!fir.ref, !fir.dscope) -> !fir.ref %4 = fir.alloca !fir.array<2xi32> {bindc_name = "jlocal", uniq_name = "_QFarrayEjlocal"} %5 = fir.declare %4(%1) {uniq_name = "_QFarrayEjlocal"} : (!fir.ref>, !fir.shape<1>) -> !fir.ref> fir.store %arrayval to %5 : !fir.ref> %val_load = fir.load %5 : !fir.ref> fir.store %val_load to %2 : !fir.ref> return } // ----- // CHECK-LABEL: func.func @box_not_mem2reg( // CHECK: fir.alloca !fir.box // CHECK: fir.store // CHECK: fir.load // CHECK: fir.store func.func @box_not_mem2reg(%arg0: !fir.ref> {fir.bindc_name = "i"}, %arg1: !fir.ref {fir.bindc_name = "j"}, %arrayval : !fir.box) { %0 = fir.dummy_scope : !fir.dscope %2 = fir.declare %arg0 dummy_scope %0 arg 1 {uniq_name = "_QFarrayEi"} : (!fir.ref>, !fir.dscope) -> !fir.ref> %3 = fir.declare %arg1 dummy_scope %0 arg 2 {uniq_name = "_QFarrayEj"} : (!fir.ref, !fir.dscope) -> !fir.ref %4 = fir.alloca !fir.box {bindc_name = "jlocal", uniq_name = "_QFarrayEjlocal"} %5 = fir.declare %4 {uniq_name = "_QFarrayEjlocal"} : (!fir.ref>) -> !fir.ref> fir.store %arrayval to %5 : !fir.ref> %val_load = fir.load %5 : !fir.ref> fir.store %val_load to %2 : !fir.ref> return } // ----- // Conditional store in a different block through fir.declare is not promoted // because MLIR mem2reg would not place the needed phi nodes correctly. // CHECK-LABEL: func.func @block_argument_value( // CHECK-SAME: %[[ARG0:.*]]: i32, // CHECK-SAME: %[[ARG1:.*]]: i1) -> i32 { // CHECK: fir.alloca i32 // CHECK: fir.declare // CHECK: fir.store // CHECK: fir.store // CHECK: fir.load func.func @block_argument_value(%arg0: i32, %cdt: i1) -> i32 { %c42_i32 = arith.constant 42 : i32 %3 = fir.alloca i32 {bindc_name = "jlocal", uniq_name = "_QFfooEjlocal"} %4 = fir.declare %3 {uniq_name = "_QFfooEjlocal"} : (!fir.ref) -> !fir.ref fir.store %c42_i32 to %4 : !fir.ref llvm.cond_br %cdt, ^bb1, ^bb2 ^bb1: fir.store %arg0 to %4 : !fir.ref llvm.br ^bb2 ^bb2: %6 = fir.load %4 : !fir.ref return %6 : i32 } // ----- // Conditional store inside a loop through fir.declare must not be promoted. // MLIR's mem2reg does not register stores through declares as defining blocks, // so phi nodes at the loop header would be missing, losing the update. // CHECK-LABEL: func.func @loop_conditional_update( // CHECK-SAME: %[[ARG0:.*]]: i32, // CHECK-SAME: %[[ARG1:.*]]: i1) -> i32 { // CHECK: fir.alloca i32 // CHECK: fir.declare // CHECK: fir.store // CHECK: fir.load // CHECK: fir.store // CHECK: fir.load func.func @loop_conditional_update(%arg0: i32, %cdt: i1) -> i32 { %c1 = arith.constant 1 : i32 %alloca = fir.alloca i32 {bindc_name = "mywatch", uniq_name = "_QFkernelEmywatch"} %declare = fir.declare %alloca {uniq_name = "_QFkernelEmywatch"} : (!fir.ref) -> !fir.ref fir.store %arg0 to %declare : !fir.ref llvm.br ^loop ^loop: %val = fir.load %declare : !fir.ref llvm.cond_br %cdt, ^update, ^exit ^update: %new = arith.subi %val, %c1 : i32 fir.store %new to %declare : !fir.ref llvm.br ^loop ^exit: %result = fir.load %declare : !fir.ref return %result : i32 } // ----- // Make sure we do not generate fir.declare_value for a replaced value // fir.declare with dummy_scope. This can result in the declare_value being // inserted before the dummy_scope it uses as would be the case here. // CHECK-LABEL: func.func @dummy_scope( // CHECK-NOT: fir.declare_value func.func @dummy_scope(%arg : i32) { %alloca = fir.alloca i32 {adapt.valuebyref} fir.store %arg to %alloca : !fir.ref %scope = fir.dummy_scope : !fir.dscope %declare = fir.declare %alloca dummy_scope %scope arg 1 {fortran_attrs = #fir.var_attrs, uniq_name = "foo"} : (!fir.ref, !fir.dscope) -> !fir.ref %result = fir.load %declare : !fir.ref fir.call @use(%result) : (i32) -> () return }