Skip to content

[Flang] PURE procedures incorrectly accept calls to impure intrinsics #157124

@mlir-maiden

Description

@mlir-maiden

It seems that Flang does not enforce restrictions on usage of impure intrinsics inside PURE procedures. Based on my tests, programs that gfortran correctly rejects as non-conforming compile and run successfully in Flang. This is a standards-conformance gap.

Example
Using CPU_TIME (impure subroutine):

program get_cpu_time
  print *, measure_cpu_time()
contains
  pure function measure_cpu_time() result(elapsed_time)
    real :: elapsed_time
    real :: time_start, time_end

    call cpu_time(time_start)   ! impure intrinsic inside PURE
    call cpu_time(time_end)     

    elapsed_time = time_end - time_start
  end function measure_cpu_time
end program get_cpu_time

Result in gfortran
Error: Subroutine call to intrinsic 'cpu_time' at (1) is not PURE

Result in flang
Compiles and runs, prints a time value.

Other intrinsics tested
The same behavior was observed with:

  • SYSTEM_CLOCK
  • DATE_AND_TIME
  • ETIME (subroutine + function forms)
  • SECNDS

It seems that Flang's checks for PURE procedures do not currently consult intrinsic classifications, allowing impure intrinsics to slip through.

Proposed Fix
Update semantic checks for PURE procedures to reject calls to intrinsics classified as impure.
I am a beginner contributor, but I would be happy to help with a patch once I have guidance on where in the semantics pipeline this check should be added. Thank you

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions