[all-commits] [llvm/llvm-project] c2fa65: [flang] Diagnose integer MOD/MODULO when the divis...

junfengd-nv via All-commits all-commits at lists.llvm.org
Tue Sep 15 15:27:52 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: c2fa65d33f124526bfe61d5125e203547076f4ce
      https://github.com/llvm/llvm-project/commit/c2fa65d33f124526bfe61d5125e203547076f4ce
  Author: junfengd-nv <junfengd at nvidia.com>
  Date:   2026-09-15 (Tue, 15 Sep 2026)

  Changed paths:
    M clang/include/clang/Options/FlangOptions.td
    M clang/lib/Driver/ToolChains/Flang.cpp
    M flang/include/flang/Optimizer/Dialect/FIRAttr.h
    M flang/include/flang/Support/LangOptions.def
    M flang/lib/Frontend/CompilerInvocation.cpp
    M flang/lib/Frontend/FrontendActions.cpp
    M flang/lib/Optimizer/Builder/IntrinsicCall.cpp
    A flang/test/Driver/check-integer-mod-zero-divisor.f90
    M flang/test/Lower/Intrinsics/mod.f90
    M flang/test/Lower/Intrinsics/modulo.f90
    M flang/test/Lower/dummy-procedure.f90

  Log Message:
  -----------
  [flang] Diagnose integer MOD/MODULO when the divisor is zero (#222790)

Flang currently prints a garbage integer when the divisor of integer
MOD/MODULO is zero. For example:
```
program mod0_var
  integer :: a, p, r
  a = 5
  p = 0
  r = mod(a, p)
  print *, r
end program
```
Integer MOD/MODULO lower to arith.remsi. A zero divisor is undefined
behavior in LLVM, so the remainder can become poison and the program
neither crashes nor reports an error.

This change checks the second argument P when lowering integer
MOD/MODULO. If P is not a compile-time nonzero constant, a P == 0 test
is inserted. On that path, _FortranAReportFatalUserError is called with
the same messages as IntMod in flang-rt (MOD with P==0 / MODULO with
P==0). The fast path remains arith.remsi.

Before:
```
func.func private @fir.mod.i32.ref_i32.ref_i32(%arg0, %arg1) {
  %a = fir.load %arg0
  %p = fir.load %arg1
  %r = arith.remsi %a, %p : i32
  return %r
}
```

After:
```
func.func private @fir.mod.i32.ref_i32.ref_i32(%arg0, %arg1) {
  %a = fir.load %arg0
  %p = fir.load %arg1
  %zero = arith.constant 0 : i32
  %bad = arith.cmpi eq, %p, %zero : i32
  fir.if %bad {
    // fatal: same text as IntMod in
    // llvm-project/flang-rt/lib/runtime/numeric.cpp
  }
  %r = arith.remsi %a, %p : i32
  return %r
}
```



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list