266 lines · plain
1// RUN: mlir-opt %s --convert-arith-to-apfloat -split-input-file -verify-diagnostics | FileCheck %s2 3// CHECK-LABEL: func.func private @_mlir_apfloat_add(i32, i64, i64) -> i644 5// CHECK-LABEL: func.func @foo() -> f8E4M3FN {6// CHECK: %[[CONSTANT_0:.*]] = arith.constant 2.250000e+00 : f8E4M3FN7// CHECK: return %[[CONSTANT_0]] : f8E4M3FN8// CHECK: }9 10// CHECK-LABEL: func.func @bar() -> f6E3M2FN {11// CHECK: %[[CONSTANT_0:.*]] = arith.constant 3.000000e+00 : f6E3M2FN12// CHECK: return %[[CONSTANT_0]] : f6E3M2FN13// CHECK: }14 15// Illustrate that both f8E4M3FN and f6E3M2FN calling the same _mlir_apfloat_add is fine16// because each gets its own semantics enum and gets bitcast/extui/trunci to its own width.17// CHECK-LABEL: func.func @full_example() {18// CHECK: %[[CONSTANT_0:.*]] = arith.constant 1.375000e+00 : f8E4M3FN19// CHECK: %[[VAL_0:.*]] = call @foo() : () -> f8E4M3FN20// CHECK: %[[BITCAST_0:.*]] = arith.bitcast %[[CONSTANT_0]] : f8E4M3FN to i821// CHECK: %[[EXTUI_0:.*]] = arith.extui %[[BITCAST_0]] : i8 to i6422// CHECK: %[[BITCAST_1:.*]] = arith.bitcast %[[VAL_0]] : f8E4M3FN to i823// CHECK: %[[EXTUI_1:.*]] = arith.extui %[[BITCAST_1]] : i8 to i6424// // fltSemantics semantics for f8E4M3FN25// CHECK: %[[CONSTANT_1:.*]] = arith.constant 10 : i3226// CHECK: %[[VAL_1:.*]] = call @_mlir_apfloat_add(%[[CONSTANT_1]], %[[EXTUI_0]], %[[EXTUI_1]]) : (i32, i64, i64) -> i6427// CHECK: %[[TRUNCI_0:.*]] = arith.trunci %[[VAL_1]] : i64 to i828// CHECK: %[[BITCAST_2:.*]] = arith.bitcast %[[TRUNCI_0]] : i8 to f8E4M3FN29// CHECK: vector.print %[[BITCAST_2]] : f8E4M3FN30 31// CHECK: %[[CONSTANT_2:.*]] = arith.constant 2.500000e+00 : f6E3M2FN32// CHECK: %[[VAL_2:.*]] = call @bar() : () -> f6E3M2FN33// CHECK: %[[BITCAST_3:.*]] = arith.bitcast %[[CONSTANT_2]] : f6E3M2FN to i634// CHECK: %[[EXTUI_2:.*]] = arith.extui %[[BITCAST_3]] : i6 to i6435// CHECK: %[[BITCAST_4:.*]] = arith.bitcast %[[VAL_2]] : f6E3M2FN to i636// CHECK: %[[EXTUI_3:.*]] = arith.extui %[[BITCAST_4]] : i6 to i6437// // fltSemantics semantics for f6E3M2FN38// CHECK: %[[CONSTANT_3:.*]] = arith.constant 16 : i3239// CHECK: %[[VAL_3:.*]] = call @_mlir_apfloat_add(%[[CONSTANT_3]], %[[EXTUI_2]], %[[EXTUI_3]]) : (i32, i64, i64) -> i6440// CHECK: %[[TRUNCI_1:.*]] = arith.trunci %[[VAL_3]] : i64 to i641// CHECK: %[[BITCAST_5:.*]] = arith.bitcast %[[TRUNCI_1]] : i6 to f6E3M2FN42// CHECK: vector.print %[[BITCAST_5]] : f6E3M2FN43// CHECK: return44// CHECK: }45 46// Put rhs into separate function so that it won't be constant-folded.47func.func @foo() -> f8E4M3FN {48 %cst = arith.constant 2.2 : f8E4M3FN49 return %cst : f8E4M3FN50}51 52func.func @bar() -> f6E3M2FN {53 %cst = arith.constant 3.2 : f6E3M2FN54 return %cst : f6E3M2FN55}56 57func.func @full_example() {58 %a = arith.constant 1.4 : f8E4M3FN59 %b = func.call @foo() : () -> (f8E4M3FN)60 %c = arith.addf %a, %b : f8E4M3FN61 vector.print %c : f8E4M3FN62 63 %d = arith.constant 2.4 : f6E3M2FN64 %e = func.call @bar() : () -> (f6E3M2FN)65 %f = arith.addf %d, %e : f6E3M2FN66 vector.print %f : f6E3M2FN67 return68}69 70// -----71 72// CHECK: func.func private @_mlir_apfloat_add(i32, i64, i64) -> i6473// CHECK: %[[sem:.*]] = arith.constant 18 : i3274// CHECK: call @_mlir_apfloat_add(%[[sem]], %{{.*}}, %{{.*}}) : (i32, i64, i64) -> i6475func.func @addf(%arg0: f4E2M1FN, %arg1: f4E2M1FN) {76 %0 = arith.addf %arg0, %arg1 : f4E2M1FN77 return78}79 80// -----81 82// Test decl collision (different type)83// expected-error@+1{{matched function '_mlir_apfloat_add' but with different type: '(i32, i32, f32) -> index' (expected '(i32, i64, i64) -> i64')}}84func.func private @_mlir_apfloat_add(i32, i32, f32) -> index85func.func @addf(%arg0: f4E2M1FN, %arg1: f4E2M1FN) {86 %0 = arith.addf %arg0, %arg1 : f4E2M1FN87 return88}89 90// -----91 92// CHECK: func.func private @_mlir_apfloat_subtract(i32, i64, i64) -> i6493// CHECK: %[[sem:.*]] = arith.constant 18 : i3294// CHECK: call @_mlir_apfloat_subtract(%[[sem]], %{{.*}}, %{{.*}}) : (i32, i64, i64) -> i6495func.func @subf(%arg0: f4E2M1FN, %arg1: f4E2M1FN) {96 %0 = arith.subf %arg0, %arg1 : f4E2M1FN97 return98}99 100// -----101 102// CHECK: func.func private @_mlir_apfloat_multiply(i32, i64, i64) -> i64103// CHECK: %[[sem:.*]] = arith.constant 18 : i32104// CHECK: call @_mlir_apfloat_multiply(%[[sem]], %{{.*}}, %{{.*}}) : (i32, i64, i64) -> i64105func.func @subf(%arg0: f4E2M1FN, %arg1: f4E2M1FN) {106 %0 = arith.mulf %arg0, %arg1 : f4E2M1FN107 return108}109 110// -----111 112// CHECK: func.func private @_mlir_apfloat_divide(i32, i64, i64) -> i64113// CHECK: %[[sem:.*]] = arith.constant 18 : i32114// CHECK: call @_mlir_apfloat_divide(%[[sem]], %{{.*}}, %{{.*}}) : (i32, i64, i64) -> i64115func.func @subf(%arg0: f4E2M1FN, %arg1: f4E2M1FN) {116 %0 = arith.divf %arg0, %arg1 : f4E2M1FN117 return118}119 120// -----121 122// CHECK: func.func private @_mlir_apfloat_remainder(i32, i64, i64) -> i64123// CHECK: %[[sem:.*]] = arith.constant 18 : i32124// CHECK: call @_mlir_apfloat_remainder(%[[sem]], %{{.*}}, %{{.*}}) : (i32, i64, i64) -> i64125func.func @remf(%arg0: f4E2M1FN, %arg1: f4E2M1FN) {126 %0 = arith.remf %arg0, %arg1 : f4E2M1FN127 return128}129 130// -----131 132// CHECK: func.func private @_mlir_apfloat_convert(i32, i32, i64) -> i64133// CHECK: %[[sem_in:.*]] = arith.constant 18 : i32134// CHECK: %[[sem_out:.*]] = arith.constant 2 : i32135// CHECK: call @_mlir_apfloat_convert(%[[sem_in]], %[[sem_out]], %{{.*}}) : (i32, i32, i64) -> i64136func.func @extf(%arg0: f4E2M1FN) {137 %0 = arith.extf %arg0 : f4E2M1FN to f32138 return139}140 141// -----142 143// CHECK: func.func private @_mlir_apfloat_convert(i32, i32, i64) -> i64144// CHECK: %[[sem_in:.*]] = arith.constant 1 : i32145// CHECK: %[[sem_out:.*]] = arith.constant 18 : i32146// CHECK: call @_mlir_apfloat_convert(%[[sem_in]], %[[sem_out]], %{{.*}}) : (i32, i32, i64) -> i64147func.func @truncf(%arg0: bf16) {148 %0 = arith.truncf %arg0 : bf16 to f4E2M1FN149 return150}151 152// -----153 154// CHECK: func.func private @_mlir_apfloat_convert_to_int(i32, i32, i1, i64) -> i64155// CHECK: %[[sem_in:.*]] = arith.constant 0 : i32156// CHECK: %[[out_width:.*]] = arith.constant 4 : i32157// CHECK: %[[is_unsigned:.*]] = arith.constant false158// CHECK: %[[res:.*]] = call @_mlir_apfloat_convert_to_int(%[[sem_in]], %[[out_width]], %[[is_unsigned]], %{{.*}}) : (i32, i32, i1, i64) -> i64159// CHECK: arith.trunci %[[res]] : i64 to i4160func.func @fptosi(%arg0: f16) {161 %0 = arith.fptosi %arg0 : f16 to i4162 return163}164 165// -----166 167// CHECK: func.func private @_mlir_apfloat_convert_to_int(i32, i32, i1, i64) -> i64168// CHECK: %[[sem_in:.*]] = arith.constant 0 : i32169// CHECK: %[[out_width:.*]] = arith.constant 4 : i32170// CHECK: %[[is_unsigned:.*]] = arith.constant true171// CHECK: %[[res:.*]] = call @_mlir_apfloat_convert_to_int(%[[sem_in]], %[[out_width]], %[[is_unsigned]], %{{.*}}) : (i32, i32, i1, i64) -> i64172// CHECK: arith.trunci %[[res]] : i64 to i4173func.func @fptoui(%arg0: f16) {174 %0 = arith.fptoui %arg0 : f16 to i4175 return176}177 178// -----179 180// CHECK: func.func private @_mlir_apfloat_convert_from_int(i32, i32, i1, i64) -> i64181// CHECK: %[[sem_out:.*]] = arith.constant 18 : i32182// CHECK: %[[in_width:.*]] = arith.constant 32 : i32183// CHECK: %[[is_unsigned:.*]] = arith.constant false184// CHECK: %[[res:.*]] = call @_mlir_apfloat_convert_from_int(%[[sem_out]], %[[in_width]], %[[is_unsigned]], %{{.*}}) : (i32, i32, i1, i64) -> i64185func.func @sitofp(%arg0: i32) {186 %0 = arith.sitofp %arg0 : i32 to f4E2M1FN187 return188}189 190// -----191 192// CHECK: func.func private @_mlir_apfloat_convert_from_int(i32, i32, i1, i64) -> i64193// CHECK: %[[sem_out:.*]] = arith.constant 18 : i32194// CHECK: %[[in_width:.*]] = arith.constant 32 : i32195// CHECK: %[[is_unsigned:.*]] = arith.constant true196// CHECK: %[[res:.*]] = call @_mlir_apfloat_convert_from_int(%[[sem_out]], %[[in_width]], %[[is_unsigned]], %{{.*}}) : (i32, i32, i1, i64) -> i64197func.func @uitofp(%arg0: i32) {198 %0 = arith.uitofp %arg0 : i32 to f4E2M1FN199 return200}201 202// -----203 204// CHECK: func.func private @_mlir_apfloat_compare(i32, i64, i64) -> i8205// CHECK: %[[sem:.*]] = arith.constant 18 : i32206// CHECK: %[[cmp:.*]] = call @_mlir_apfloat_compare(%[[sem]], %{{.*}}, %{{.*}}) : (i32, i64, i64) -> i8207// CHECK: %[[c3:.*]] = arith.constant 3 : i8208// CHECK: %[[is_unordered:.*]] = arith.cmpi eq, %[[cmp]], %[[c3]] : i8209// CHECK: %[[c0:.*]] = arith.constant 0 : i8210// CHECK: %[[is_lt:.*]] = arith.cmpi eq, %[[cmp]], %[[c0]] : i8211// CHECK: arith.ori %[[is_unordered]], %[[is_lt]] : i1212func.func @cmpf(%arg0: f4E2M1FN, %arg1: f4E2M1FN) {213 %0 = arith.cmpf "ult", %arg0, %arg1 : f4E2M1FN214 return215}216 217// -----218 219// CHECK: func.func private @_mlir_apfloat_neg(i32, i64) -> i64220// CHECK: %[[sem:.*]] = arith.constant 2 : i32221// CHECK: %[[res:.*]] = call @_mlir_apfloat_neg(%[[sem]], %{{.*}}) : (i32, i64) -> i64222func.func @negf(%arg0: f32) {223 %0 = arith.negf %arg0 : f32224 return225}226 227// -----228 229// CHECK: func.func private @_mlir_apfloat_minimum(i32, i64, i64) -> i64230// CHECK: %[[sem:.*]] = arith.constant 2 : i32231// CHECK: %[[res:.*]] = call @_mlir_apfloat_minimum(%[[sem]], %{{.*}}, %{{.*}}) : (i32, i64, i64) -> i64232func.func @minimumf(%arg0: f32, %arg1: f32) {233 %0 = arith.minimumf %arg0, %arg1 : f32234 return235}236 237// -----238 239// CHECK: func.func private @_mlir_apfloat_maximum(i32, i64, i64) -> i64240// CHECK: %[[sem:.*]] = arith.constant 2 : i32241// CHECK: %[[res:.*]] = call @_mlir_apfloat_maximum(%[[sem]], %{{.*}}, %{{.*}}) : (i32, i64, i64) -> i64242func.func @maximumf(%arg0: f32, %arg1: f32) {243 %0 = arith.maximumf %arg0, %arg1 : f32244 return245}246 247// -----248 249// CHECK: func.func private @_mlir_apfloat_minnum(i32, i64, i64) -> i64250// CHECK: %[[sem:.*]] = arith.constant 2 : i32251// CHECK: %[[res:.*]] = call @_mlir_apfloat_minnum(%[[sem]], %{{.*}}, %{{.*}}) : (i32, i64, i64) -> i64252func.func @minnumf(%arg0: f32, %arg1: f32) {253 %0 = arith.minnumf %arg0, %arg1 : f32254 return255}256 257// -----258 259// CHECK: func.func private @_mlir_apfloat_maxnum(i32, i64, i64) -> i64260// CHECK: %[[sem:.*]] = arith.constant 2 : i32261// CHECK: %[[res:.*]] = call @_mlir_apfloat_maxnum(%[[sem]], %{{.*}}, %{{.*}}) : (i32, i64, i64) -> i64262func.func @maxnumf(%arg0: f32, %arg1: f32) {263 %0 = arith.maxnumf %arg0, %arg1 : f32264 return265}266