[flang-dev] Intrinsic function with an optional TEAM_TYPE dummy argument

Jean Perier via flang-dev flang-dev at lists.llvm.org
Tue May 11 11:15:31 PDT 2021


Hi Craig,

Having a different signature in the table is an interesting idea, however it might lead to some trouble in lowering that expects to find the Fortran standard signatures (at least in terms of number of arguments and names).
An alternative would be to allow partial characterization of absent arguments in evaluate::DummyDataObject. After all, it may not always be possible to come with a type and a shape for an absent argument. It is not very clear to me that setting an exact type and shape of absent arguments in the characteristics built for intrinsic procedures (evaluate::characteristics::Procedure)  is useful. It might be worth checking if the information is actually needed in this case, and if not, to add a way to indicate that the exact characteristic of a DummyDataObject is unknown, and save the trouble of trying to build a type-spec for it.

Jean

From: Rasmussen, Craig E. <rasmussen17 at llnl.gov>
Sent: Monday, May 10, 2021 9:18 PM
To: Jean Perier <jperier at nvidia.com>
Cc: flang-dev at lists.llvm.org
Subject: Re: [flang-dev] Intrinsic function with an optional TEAM_TYPE dummy argument

External email: Use caution opening links or attachments

Hi Jean,

I'm actually happy to do this as long as it makes sense.  I've been really curious to know about how symbols and scopes work anyway.

An alternative would be to have multiple entries in intrinsics.cpp for the optional team dummy arguments.  One entry for no team and one entry for a required team arg.  However, this is a bit of a hack.  I think I tried this out initially to see if it would work but I actually can't remember if it fully does.

Whatever, let me know what you all think is the best way to proceed and I'll muddle through.

Thanks,
Craig

From: Jean Perier <jperier at nvidia.com<mailto:jperier at nvidia.com>>
Date: Friday, May 7, 2021 at 9:31 AM
To: Rasmussen, Craig E. <rasmussen17 at llnl.gov<mailto:rasmussen17 at llnl.gov>>
Cc: flang-dev at lists.llvm.org<mailto:flang-dev at lists.llvm.org> <flang-dev at lists.llvm.org<mailto:flang-dev at lists.llvm.org>>
Subject: RE: [flang-dev] Intrinsic function with an optional TEAM_TYPE dummy argument
Hi Craig,

Yes, the frontend should have seen iso_fortran_env if team_number is used, so you are right that it's possible to get the type from that.

The idea would be to go through the module scopes under the global scope (accessible via context.globalScope(), where context is the SemanticsContext). Then you could look inside the builtin modules for a symbol named "__builtin_team_type". With that type symbol, you should be able to create a semantics::DerivedTypeSpec, and then a semantics::DerivedTypeSpec (For the DerivedTypeSpec  sourceName, usually it would be the source position of the type-spec that relates to the object declaration, but here you can probably just use the one from the type symbol.).

You may want to make the return of this helper function optional and emit an error on the client side in case the modules were not loaded for some reasons, though I am not entirely sure if this can happen.

You should be able to find how to do all that looking at semantics/scope.h, semantics/type.h and the implementation of semantics::IsBuiltinDerivedType (in semantics/tools.cpp). I think it can give you an interesting overview of how the front-end type system, symbols and scopes work, but if you do not want to deal with this part, let me know and I'll give it a try.

Jean

From: flang-dev <flang-dev-bounces at lists.llvm.org<mailto:flang-dev-bounces at lists.llvm.org>> On Behalf Of Rasmussen, Craig E. via flang-dev
Sent: Thursday, May 6, 2021 10:22 PM
To: flang-dev at lists.llvm.org<mailto:flang-dev at lists.llvm.org>
Subject: Re: [flang-dev] Intrinsic function with an optional TEAM_TYPE dummy argument

External email: Use caution opening links or attachments

Hi Jean,

Nope, what I tried didn't work.  It looks like there is no current way to construct a characteristics::DummyArgument (of variant characteristics::DummyDataObject) without a TypeAndShape object and ultimately requiring a semantics::DerivedTypeSpec for the team type.

As you said,
TEAM_TYPE, like all derived types defined by the standard, are defined by means of Fortran source files (see module/iso_fortran_env.f90 and module/__fortran_builtins.f90), so the compiler has no knowledge of what these derived types actually are without access to these source/module files (For user objects, these module files are made accessible by the user via use statements to iso_fortran_env).

The frontend will already have seen the iso_fortran_env source file so I'm wondering is special accessor functions could be provided with access to these types?

(Yikes, if TEAM_TYPE is this hard I'm really starting to worry about the array descriptor for coarrays.)

Cheers,
Craig

From: Rasmussen, Craig E. <rasmussen17 at llnl.gov<mailto:rasmussen17 at llnl.gov>>
Date: Thursday, May 6, 2021 at 11:42 AM
To: flang-dev at lists.llvm.org<mailto:flang-dev at lists.llvm.org> <flang-dev at lists.llvm.org<mailto:flang-dev at lists.llvm.org>>
Subject: Re: [flang-dev] Intrinsic function with an optional TEAM_TYPE dummy argument
Hi Jean,

Thanks for responding.

You asked:
By "TypeAndShape constructor calls GetDefaultKind()", you mean that GetDefaultKind is called in the arguments of a TypeAndShape constructor, not inside the TypeAndShape constructor function itself right?
Correct, this is exactly what I was talking about though perhaps awkwardly worded.

Re.
So going that way, I think you would need a semantics::DerivedTypeSpec for the team type. I am not sure there currently is a way to generate such DerivedTypeSpec without a user object (which I think would be your case, since there is no actual argument to get the type from).

Yeah, I tried to go down this route and reached the same conclusion. If the optional argument is missing there won't be an actual argument. Kind of painful.

It just doesn't feel to me like DynamicType is the right approach; we aren't talking about polymorphic types in iso_fortran_env I don't think.  I had wanted to do something like what happens when d.typePattern.kindCode == KindCode::same, however, as I recall,

     dummyArgs.emplace_back(dummyArgs[sameDummyArg.value()])

failed because of a problem with sameDummyArg.value().  If I understand the code correctly, nothing complicated is going on, we are just trying to put the same stupid dummy argument back on the list.  Hum, I just looked at this some more and I have some ideas I would like to try.

Thanks again,
Craig

From: Jean Perier <jperier at nvidia.com<mailto:jperier at nvidia.com>>
Date: Thursday, May 6, 2021 at 5:09 AM
To: Rasmussen, Craig E. <rasmussen17 at llnl.gov<mailto:rasmussen17 at llnl.gov>>
Cc: flang-dev at lists.llvm.org<mailto:flang-dev at lists.llvm.org> <flang-dev at lists.llvm.org<mailto:flang-dev at lists.llvm.org>>
Subject: RE: [flang-dev] Intrinsic function with an optional TEAM_TYPE dummy argument
Hi Craig,

I think you may be the first having to deal with optional derived type arguments in intrinsic functions. First, I would like to clarify where you are having an issue with IntrinsicTypeDefaultKinds::GetDefaultKind being called with a derived type category after your first three changes.
By "TypeAndShape constructor calls GetDefaultKind()", you mean that GetDefaultKind is called in the arguments of a TypeAndShape constructor, not inside the TypeAndShape constructor function itself right ?

Is this at: https://github.com/llvm/llvm-project/blob/d8805574c183484f055552855fa82d2e8932415e/flang/lib/Evaluate/intrinsics.cpp#L1666<https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Furldefense.us%2Fv3%2F__https%3A%2Fnam11.safelinks.protection.outlook.com%2F%3Furl%3Dhttps*3A*2F*2Furldefense.us*2Fv3*2F__https*3A*2Fgithub.com*2Fllvm*2Fllvm-project*2Fblob*2Fd8805574c183484f055552855fa82d2e8932415e*2Fflang*2Flib*2FEvaluate*2Fintrinsics.cpp*L1666__*3BIw!!G2kpM7uM-TzIFchu!hDkkoTDXWz2D4JfoCRcVEbwKxSmmoS0YhydSdaMxZC-GeYOgp-LFZ4r-5A_Kb59ZX9OV*24%26data%3D04*7C01*7Cjperier*40nvidia.com*7C4173d63013e549e1a74508d910d02033*7C43083d15727340c1b7db39efd9ccc17a*7C0*7C0*7C637559308353826720*7CUnknown*7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0*3D*7C1000%26sdata%3DmpnbJYG62UaErqN9yRE0oPJNi8KA3ugTK398ou9LtBY*3D%26reserved%3D0__%3BJSUlJSUlJSUlJSUlJSUlKiUlJSUlJSUlJSUlJSUlJQ!!G2kpM7uM-TzIFchu!h6zRU9VZyBMTknDtW-lI8JtNYpRyjsc1mpKGdOGnSxHSZhweetCxAEoW7B1OiS1Yc3nS%24&data=04%7C01%7Cjperier%40nvidia.com%7C208e3587f3ed40da528108d913e86496%7C43083d15727340c1b7db39efd9ccc17a%7C0%7C0%7C637562711118106098%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=g7TXiS3M36p05Gojizm3%2Bs0YNXHs%2BUp2xuTMBOgVOoU%3D&reserved=0> ?

} else {
      // optional argument is absent
      CHECK(d.optionality != Optionality::required);
      if (d.typePattern.kindCode == KindCode::same) {
        dummyArgs.emplace_back(dummyArgs[sameDummyArg.value()]);
      } else {
        auto category{d.typePattern.categorySet.LeastElement().value()};
        characteristics::TypeAndShape typeAndShape{
            DynamicType{category, defaults.GetDefaultKind(category)}};
        dummyArgs.emplace_back(std::string{d.keyword},
            characteristics::DummyDataObject{std::move(typeAndShape)});

Looking at the code you propose, the overall idea looks good to me, the issue is that DynamicType cannot be created from the simply the TypeCategory for derived type. A semantics::DerivedTypeSpec that precisely described the derived type specification of a derived type entity needs to be provided (DynamicType must be different for objects of different derived types, there cannot be a single DynamicType for all derived types).

So going that way, I think you would need a semantics::DerivedTypeSpec for the team type. I am not sure there currently is a way to generate such DerivedTypeSpec without a user object (which I think would be your case, since there is no actual argument to get the type from). That is because TEAM_TYPE, like all derived types defined by the standard, are defined by means of Fortran source files (see module/iso_fortran_env.f90 and module/__fortran_builtins.f90), so the compiler has no knowledge of what these derived types actually are without access to these source/module files (For user objects, these module files are made accessible by the user via use statements to iso_fortran_env).

Jean

From: flang-dev <flang-dev-bounces at lists.llvm.org<mailto:flang-dev-bounces at lists.llvm.org>> On Behalf Of Rasmussen, Craig E. via flang-dev
Sent: Monday, May 3, 2021 10:39 PM
To: flang-dev at lists.llvm.org<mailto:flang-dev at lists.llvm.org>
Subject: [flang-dev] Intrinsic function with an optional TEAM_TYPE dummy argument

External email: Use caution opening links or attachments

I'm trying to implement team_number(team) in lib/Evaluate/intrinsics.cpp and I'm running into a problem.  The issue is how to handle team_type from the intrinsic module iso_fortran_env.  I'll outline the steps I've taken so far and then describe the problem I'm having.

I've:
  1. Changed the CategorySet in TypePattern TEAM_TYPE from IntType to DerivedType. I'm pretty sure this is the correct thing to do.
  2. Added team_number to the IntrinsicInterface list with an optional TEAM_TYPE. Again, no problem here.
  3. Implemented checking types and kinds of the actual argument against the team_number's interface. This is pretty straight forward:
         case KindCode::teamType:
           argOk = semantics::IsTeamType(GetDerivedTypeSpec(type));

The problem occurs when the code tries to check the dummy argument (team) for the case in which the optional actual argument is missing. The normal control flow path that this would take would be to move a characteristics::DummyDataObject (constructed with characteristics::TypeAndShape) onto the dummyArgs list. However, the TypeAndShape constructor calls GetDefaultKind() with a TEAM_TYPE category argument (which is TypeCategory::DerivedType).

Therein lies the problem. The function IntrinsicTypeDefaultKinds::GetDefaultKind(TypeCategory::DerivedType) fails with a hard error because a derived type is not an intrinsic type. At first I was thoroughly confused by the whole type kind thing for a derived type, specifically one from an intrinsic module. Then as a last (of course) resort, I checked the standard more carefully and found Note 16.34 which states that types in intrinsic modules are not themselves intrinsic. Thus IntrinsicTypeDefaultKinds::GetDefaultKind(TypeCategory::DerivedType) should fail.

So the conclusion is that I need help fixing this (for function team_type() and several other intrinsic functions with a dummy argument of TEAM_TYPE). The code could look something like the following (at roughly line 1670 in intrinsics.cpp):

      } else if (d.typePattern.kindCode == KindCode::teamType) {
        characteristics::TypeAndShape typeAndShape{DynamicType{TypeCategory::Derived}};
        dummyArgs.emplace_back(std::string{d.keyword},
            characteristics::DummyDataObject{std::move(typeAndShape)});
      }

However, there is no constructor for TypeAndShape with a single argument of type TypeCategory::Derived. After staring at many lines of code in flang I'm left wondering if there shouldn't be?

Thanks,
Craig

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/flang-dev/attachments/20210511/dabfdc91/attachment-0001.html>


More information about the flang-dev mailing list