[flang] Do not create arith.extui with same from/to type
authorValentin Clement (バレンタイン クレメン) <clementval@gmail.com>
Sun, 24 Apr 2022 18:37:27 +0000 (20:37 +0200)
committerValentin Clement <clementval@gmail.com>
Sun, 24 Apr 2022 18:37:48 +0000 (20:37 +0200)
In some case the lowering of `ichar` is generating an `arith.extui` operation
with the same from/to type. This operation do not accept from/to types to be
the same. If the from/to types are identical, we do not generate the extra
operation.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D124107

flang/lib/Lower/IntrinsicCall.cpp
flang/test/Lower/Intrinsics/ichar.f90

index cf68b14..957a759 100644 (file)
@@ -2614,6 +2614,8 @@ IntrinsicLibrary::genIchar(mlir::Type resultType,
   }
   LLVM_DEBUG(llvm::dbgs() << "ichar(" << charVal << ")\n");
   auto code = helper.extractCodeFromSingleton(charVal);
+  if (code.getType() == resultType)
+    return code;
   return builder.create<mlir::arith::ExtUIOp>(loc, resultType, code);
 }
 
index 2597d47..e99fae0 100644 (file)
@@ -31,3 +31,13 @@ subroutine ichar_test(c)
   ! CHECK-NEXT: fir.call @{{.*}}EndIoStatement
   print *, iachar('X')
 end subroutine
+
+! Check that 'arith.extui' op is not generated if type are matching.
+ ! CHECK-LABEL: no_extui
+subroutine no_extui(ch)
+  integer, parameter :: kind = selected_char_kind('ISO_10646')
+  character(*, kind), intent(in) :: ch(:)
+  integer :: i, j 
+  ! CHECK-NOT: arith.extui
+  j = ichar(ch(i)(i:i))
+end subroutine