diff --git a/flang/lib/Lower/IntrinsicCall.cpp b/flang/lib/Lower/IntrinsicCall.cpp --- a/flang/lib/Lower/IntrinsicCall.cpp +++ b/flang/lib/Lower/IntrinsicCall.cpp @@ -2614,6 +2614,8 @@ } LLVM_DEBUG(llvm::dbgs() << "ichar(" << charVal << ")\n"); auto code = helper.extractCodeFromSingleton(charVal); + if (code.getType() == resultType) + return code; return builder.create(loc, resultType, code); } diff --git a/flang/test/Lower/Intrinsics/ichar.f90 b/flang/test/Lower/Intrinsics/ichar.f90 --- a/flang/test/Lower/Intrinsics/ichar.f90 +++ b/flang/test/Lower/Intrinsics/ichar.f90 @@ -31,3 +31,13 @@ ! 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