Skip to content

Commit f5592f3

Browse files
authored
[flang][runtime] Handle type code synonyms in CFI_... runtime (#66231)
Some CFI_type_... type codes are synonyms; ensure that they are treated as equivalent when validating inputs to CFI_... runtime routines.
1 parent 9918d25 commit f5592f3

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

flang/include/flang/Runtime/type-code.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,19 @@ class TypeCode {
5353
RT_API_ATTRS std::optional<std::pair<TypeCategory, int>>
5454
GetCategoryAndKind() const;
5555

56-
RT_API_ATTRS bool operator==(const TypeCode &that) const {
57-
return raw_ == that.raw_;
56+
RT_API_ATTRS bool operator==(TypeCode that) const {
57+
if (raw_ == that.raw_) { // fast path
58+
return true;
59+
} else {
60+
// Multiple raw CFI_type_... codes can represent the same Fortran
61+
// type category + kind type parameter, e.g. CFI_type_int and
62+
// CFI_type_int32_t.
63+
auto thisCK{GetCategoryAndKind()};
64+
auto thatCK{that.GetCategoryAndKind()};
65+
return thisCK && thatCK && *thisCK == *thatCK;
66+
}
5867
}
59-
bool operator!=(const TypeCode &that) const { return raw_ != that.raw_; }
68+
bool operator!=(TypeCode that) const { return !(*this == that); }
6069

6170
private:
6271
ISO::CFI_type_t raw_{CFI_type_other};

flang/runtime/ISO_Fortran_binding.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,11 @@ int CFI_section(CFI_cdesc_t *result, const CFI_cdesc_t *source,
148148
if (IsAssumedSize(source) && !upper_bounds) {
149149
return CFI_INVALID_DESCRIPTOR;
150150
}
151-
if ((result->type != source->type) ||
152-
(result->elem_len != source->elem_len)) {
153-
return CFI_INVALID_DESCRIPTOR;
151+
if (runtime::TypeCode{result->type} != runtime::TypeCode{source->type}) {
152+
return CFI_INVALID_TYPE;
153+
}
154+
if (source->elem_len != result->elem_len) {
155+
return CFI_INVALID_ELEM_LEN;
154156
}
155157
if (result->attribute == CFI_attribute_allocatable) {
156158
return CFI_INVALID_ATTRIBUTE;
@@ -256,7 +258,7 @@ int CFI_setpointer(CFI_cdesc_t *result, const CFI_cdesc_t *source,
256258
if (source->rank != result->rank) {
257259
return CFI_INVALID_RANK;
258260
}
259-
if (source->type != result->type) {
261+
if (runtime::TypeCode{source->type} != runtime::TypeCode{result->type}) {
260262
return CFI_INVALID_TYPE;
261263
}
262264
if (source->elem_len != result->elem_len) {

0 commit comments

Comments
 (0)