Skip to content

Commit ce39247

Browse files
authored
[flang] Silence spurious error on non-CUDA use of CUDA module (#107444)
When a module file has been compiled with CUDA enabled, don't emit spurious errors about non-interoperable types when that module is read by a USE statement in a later non-CUDA compilation.
1 parent d8a8eae commit ce39247

File tree

7 files changed

+24
-24
lines changed

7 files changed

+24
-24
lines changed

flang/include/flang/Semantics/type.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,5 @@ inline const DerivedTypeSpec *DeclTypeSpec::AsDerived() const {
459459
return const_cast<DeclTypeSpec *>(this)->AsDerived();
460460
}
461461

462-
std::optional<bool> IsInteroperableIntrinsicType(
463-
const DeclTypeSpec &, const common::LanguageFeatureControl &);
464-
465462
} // namespace Fortran::semantics
466463
#endif // FORTRAN_SEMANTICS_TYPE_H_

flang/lib/Evaluate/type.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,8 +820,8 @@ std::optional<bool> IsInteroperableIntrinsicType(const DynamicType &type,
820820
return true;
821821
case TypeCategory::Real:
822822
case TypeCategory::Complex:
823-
return (features && features->IsEnabled(common::LanguageFeature::CUDA)) ||
824-
type.kind() >= 4; // no short or half floats
823+
return type.kind() >= 4 /* not a short or half float */ || !features ||
824+
features->IsEnabled(common::LanguageFeature::CUDA);
825825
case TypeCategory::Logical:
826826
return type.kind() == 1; // C_BOOL
827827
case TypeCategory::Character:

flang/lib/Semantics/check-declarations.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3003,17 +3003,17 @@ parser::Messages CheckHelper::WhyNotInteroperableDerivedType(
30033003
} else {
30043004
msgs.Annex(std::move(bad));
30053005
}
3006-
} else if (!IsInteroperableIntrinsicType(
3007-
*type, context_.languageFeatures())
3006+
} else if (auto dyType{evaluate::DynamicType::From(*type)}; dyType &&
3007+
!evaluate::IsInteroperableIntrinsicType(
3008+
*dyType, &context_.languageFeatures())
30083009
.value_or(false)) {
3009-
auto maybeDyType{evaluate::DynamicType::From(*type)};
30103010
if (type->category() == DeclTypeSpec::Logical) {
30113011
if (context_.ShouldWarn(common::UsageWarning::LogicalVsCBool)) {
30123012
msgs.Say(component.name(),
30133013
"A LOGICAL component of an interoperable type should have the interoperable KIND=C_BOOL"_port_en_US);
30143014
}
3015-
} else if (type->category() == DeclTypeSpec::Character &&
3016-
maybeDyType && maybeDyType->kind() == 1) {
3015+
} else if (type->category() == DeclTypeSpec::Character && dyType &&
3016+
dyType->kind() == 1) {
30173017
if (context_.ShouldWarn(common::UsageWarning::BindCCharLength)) {
30183018
msgs.Say(component.name(),
30193019
"A CHARACTER component of an interoperable type should have length 1"_port_en_US);
@@ -3106,10 +3106,15 @@ parser::Messages CheckHelper::WhyNotInteroperableObject(const Symbol &symbol) {
31063106
type->category() == DeclTypeSpec::Character &&
31073107
type->characterTypeSpec().length().isDeferred()) {
31083108
// ok; F'2023 18.3.7 p2(6)
3109-
} else if (derived ||
3110-
IsInteroperableIntrinsicType(*type, context_.languageFeatures())
3111-
.value_or(false)) {
3109+
} else if (derived) { // type has been checked
3110+
} else if (auto dyType{evaluate::DynamicType::From(*type)}; dyType &&
3111+
evaluate::IsInteroperableIntrinsicType(*dyType,
3112+
InModuleFile() ? nullptr : &context_.languageFeatures())
3113+
.value_or(false)) {
31123114
// F'2023 18.3.7 p2(4,5)
3115+
// N.B. Language features are not passed to IsInteroperableIntrinsicType
3116+
// when processing a module file, since the module file might have been
3117+
// compiled with CUDA while the client is not.
31133118
} else if (type->category() == DeclTypeSpec::Logical) {
31143119
if (context_.ShouldWarn(common::UsageWarning::LogicalVsCBool) &&
31153120
!InModuleFile()) {

flang/lib/Semantics/expression.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1956,7 +1956,7 @@ MaybeExpr ExpressionAnalyzer::Analyze(const parser::ArrayConstructor &array) {
19561956

19571957
// Check if implicit conversion of expr to the symbol type is legal (if needed),
19581958
// and make it explicit if requested.
1959-
static MaybeExpr implicitConvertTo(const semantics::Symbol &sym,
1959+
static MaybeExpr ImplicitConvertTo(const semantics::Symbol &sym,
19601960
Expr<SomeType> &&expr, bool keepConvertImplicit) {
19611961
if (!keepConvertImplicit) {
19621962
return ConvertToType(sym, std::move(expr));
@@ -2196,7 +2196,7 @@ MaybeExpr ExpressionAnalyzer::Analyze(
21962196
// convert would cause a segfault. Lowering will deal with
21972197
// conditionally converting and preserving the lower bounds in this
21982198
// case.
2199-
if (MaybeExpr converted{implicitConvertTo(
2199+
if (MaybeExpr converted{ImplicitConvertTo(
22002200
*symbol, std::move(*value), IsAllocatable(*symbol))}) {
22012201
if (auto componentShape{GetShape(GetFoldingContext(), *symbol)}) {
22022202
if (auto valueShape{GetShape(GetFoldingContext(), *converted)}) {

flang/lib/Semantics/type.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -893,13 +893,4 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &o, const DeclTypeSpec &x) {
893893
return o << x.AsFortran();
894894
}
895895

896-
std::optional<bool> IsInteroperableIntrinsicType(
897-
const DeclTypeSpec &type, const common::LanguageFeatureControl &features) {
898-
if (auto dyType{evaluate::DynamicType::From(type)}) {
899-
return IsInteroperableIntrinsicType(*dyType, &features);
900-
} else {
901-
return std::nullopt;
902-
}
903-
}
904-
905896
} // namespace Fortran::semantics
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module usereal2
2+
!REAL(2) is interoperable under CUDA
3+
real(2), bind(c) :: x
4+
end

flang/test/Semantics/modfile66.f90

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
! RUN: %flang_fc1 -fsyntax-only %S/Inputs/modfile66.cuf && %flang_fc1 -fsyntax-only %s
2+
use usereal2 ! valid since x is not used
3+
end

0 commit comments

Comments
 (0)