Skip to content

Commit c8a7a3a

Browse files
authored
[flang][Evaluate] Add IntrinsicCall::impureFunction to RAND and IRAND (#170492)
This PR adds the` impureFunction` intrinsicClass for intrinsics wich are function such as RAND and IRAND, which are not PURE functions in the GNU extension and therefore cannot be called in a DO CONCURRENT (see `test-suite::gfortran-regression-compile-regression__pr119836_2_f90.test` ). The `Pure` attribute will not be added for these intrinsics.
1 parent 4fcb6e1 commit c8a7a3a

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

flang/include/flang/Evaluate/intrinsics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct SpecificIntrinsicFunctionInterface : public characteristics::Procedure {
6363
// Generic intrinsic classes from table 16.1
6464
ENUM_CLASS(IntrinsicClass, atomicSubroutine, collectiveSubroutine,
6565
elementalFunction, elementalSubroutine, inquiryFunction, pureSubroutine,
66-
impureSubroutine, transformationalFunction, noClass)
66+
impureFunction, impureSubroutine, transformationalFunction, noClass)
6767

6868
class IntrinsicProcTable {
6969
private:

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,8 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
657657
{"irand",
658658
{{"i", TypePattern{IntType, KindCode::exactKind, 4}, Rank::scalar,
659659
Optionality::optional}},
660-
TypePattern{IntType, KindCode::exactKind, 4}, Rank::scalar},
660+
TypePattern{IntType, KindCode::exactKind, 4}, Rank::scalar,
661+
IntrinsicClass::impureFunction},
661662
{"ishft", {{"i", SameIntOrUnsigned}, {"shift", AnyInt}}, SameIntOrUnsigned},
662663
{"ishftc",
663664
{{"i", SameIntOrUnsigned}, {"shift", AnyInt},
@@ -879,7 +880,8 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
879880
{"rand",
880881
{{"i", TypePattern{IntType, KindCode::exactKind, 4}, Rank::scalar,
881882
Optionality::optional}},
882-
TypePattern{RealType, KindCode::exactKind, 4}, Rank::scalar},
883+
TypePattern{RealType, KindCode::exactKind, 4}, Rank::scalar,
884+
IntrinsicClass::impureFunction},
883885
{"range",
884886
{{"x", AnyNumeric, Rank::anyOrAssumedRank, Optionality::required,
885887
common::Intent::In,
@@ -2834,7 +2836,8 @@ std::optional<SpecificCall> IntrinsicInterface::Match(
28342836
name, characteristics::Procedure{std::move(dummyArgs), attrs}},
28352837
std::move(rearranged)};
28362838
} else {
2837-
attrs.set(characteristics::Procedure::Attr::Pure);
2839+
if (intrinsicClass != IntrinsicClass::impureFunction /* RAND and IRAND */)
2840+
attrs.set(characteristics::Procedure::Attr::Pure);
28382841
characteristics::TypeAndShape typeAndShape{resultType.value(), resultRank};
28392842
characteristics::FunctionResult funcResult{std::move(typeAndShape)};
28402843
characteristics::Procedure chars{

flang/test/Semantics/doconcurrent01.f90

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ end function ipf
211211
type(procTypeNotPure) :: procVarNotPure
212212
type(procTypePure) :: procVarPure
213213
integer :: ivar
214+
real :: rvar
214215

215216
procVarPure%pureProcComponent => pureFunc
216217

@@ -239,6 +240,14 @@ end function ipf
239240
ivar = generic()
240241
end do
241242

243+
! This should generate an error
244+
do concurrent (i = 1:10)
245+
!ERROR: Impure procedure 'irand' may not be referenced in DO CONCURRENT
246+
ivar = irand()
247+
!ERROR: Impure procedure 'rand' may not be referenced in DO CONCURRENT
248+
rvar = rand()
249+
end do
250+
242251
contains
243252
integer function notPureFunc()
244253
notPureFunc = 2

0 commit comments

Comments
 (0)