Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HeapBufferType to represent _ddata in Dyno #26065

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions compiler/AST/wellknown.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ AggregateType* dtTuple;
// these are only used when the dyno resolver is active
AggregateType* dtCPointer;
AggregateType* dtCPointerConst;
AggregateType* dtHeapBuffer;

Type* dt_c_int;
Type* dt_c_uint;
Expand Down Expand Up @@ -201,6 +202,7 @@ static WellKnownAggregateTypeNeededEarly sWellKnownAggregateTypesNeededEarly[]=
{ "_tuple", nullptr, &dtTuple, false },
{ "c_ptr", "c_ptr", &dtCPointer, true },
{ "c_ptrConst", "c_ptrConst", &dtCPointerConst, true },
{ "_ddata", "_ddata", &dtHeapBuffer, true },
};

struct WellKnownType
Expand Down
1 change: 1 addition & 0 deletions compiler/include/wellknown.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ extern AggregateType* dtTuple;
// these are only used when the dyno resolver is active
extern AggregateType* dtCPointer;
extern AggregateType* dtCPointerConst;
extern AggregateType* dtHeapBuffer;

extern Type* dt_c_int;
extern Type* dt_c_uint;
Expand Down
23 changes: 16 additions & 7 deletions compiler/passes/convert-uast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ struct Converter {
// type conversion helpers
Type* helpConvertType(types::QualifiedType qt);
Type* convertClassType(const types::QualifiedType qt);
Type* convertCPtrType(const types::CPtrType* t);
Type* convertPtrType(const types::PtrType* t);
Type* convertEnumType(const types::QualifiedType qt);
Type* convertExternType(const types::QualifiedType qt);
Type* convertFunctionType(const types::QualifiedType qt);
Expand Down Expand Up @@ -4584,7 +4584,8 @@ Type* Converter::helpConvertType(types::QualifiedType qt) {
case typetags::IntType: return convertIntType(qt);
case typetags::RealType: return convertRealType(qt);
case typetags::UintType: return convertUintType(qt);
case typetags::CPtrType: return convertCPtrType(t->toCPtrType());
case typetags::CPtrType: return convertPtrType(t->toPtrType());
case typetags::HeapBufferType: return convertPtrType(t->toPtrType());

// implementation detail tags (should not be reachable)
case typetags::START_ManageableType:
Expand All @@ -4599,6 +4600,8 @@ Type* Converter::helpConvertType(types::QualifiedType qt) {
case typetags::END_PrimitiveType:
case typetags::START_IteratorType:
case typetags::END_IteratorType:
case typetags::START_PtrType:
case typetags::END_PtrType:
case typetags::NUM_TYPE_TAGS:
INT_FATAL("should not be reachable");
return dtUnknown;
Expand All @@ -4610,19 +4613,25 @@ Type* Converter::helpConvertType(types::QualifiedType qt) {
return nullptr;
}

Type* Converter::convertCPtrType(const types::CPtrType* t) {
// find the C pointer type to instantiate
AggregateType* base = t->isConst() ? dtCPointerConst : dtCPointer;
Type* Converter::convertPtrType(const types::PtrType* t) {
// find the pointer type to instantiate
AggregateType* base = nullptr;
if (auto ct = t->toCPtrType()) {
base = ct->isConst() ? dtCPointerConst : dtCPointer;
} else {
INT_ASSERT(t->toHeapBufferType());
base = dtHeapBuffer;
}

// handle 'c_ptr' and 'c_ptrConst' without an element type
// handle ptr without an element type
if (t->eltType() == nullptr) {
return base;
}

auto qt = types::QualifiedType(types::QualifiedType::TYPE, t);

if (base->numFields() == 0) {
// the proper AST for CPointer hasn't been created yet,
// the proper AST for the pointer type hasn't been created yet,
// and we need it to proceed, so return a temporary conversion symbol.
Type* t = new TemporaryConversionType(qt);
return t;
Expand Down
1 change: 1 addition & 0 deletions frontend/include/chpl/framework/all-global-strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ X(c_ptr , "c_ptr")
X(c_ptrConst , "c_ptrConst")
X(c_char , "c_char")
X(class_ , "class")
X(ddata_ , "_ddata")
X(defaultDist , "defaultDist")
X(deinit , "deinit")
X(deserialize , "deserialize")
Expand Down
58 changes: 10 additions & 48 deletions frontend/include/chpl/types/CPtrType.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,59 +20,35 @@
#ifndef CHPL_TYPES_CPTR_TYPE_H
#define CHPL_TYPES_CPTR_TYPE_H

#include "chpl/types/PtrType.h"
#include "chpl/types/Type.h"
#include "chpl/types/QualifiedType.h"

namespace chpl {
namespace types {

class CPtrType final : public Type {
class CPtrType final : public PtrType {
private:
const CPtrType* instantiatedFrom_;
const Type* eltType_;
const bool isConst_ = false;

CPtrType(const CPtrType* instantiatedFrom,
const Type* eltType,
CPtrType(const CPtrType* instantiatedFrom, const Type* eltType,
bool isConst = false)
: Type(typetags::CPtrType), instantiatedFrom_(instantiatedFrom),
eltType_(eltType), isConst_(isConst) {
// not an instantiation -> eltType_ should be empty
CHPL_ASSERT(instantiatedFrom_ != nullptr || eltType_ == nullptr);
// is an instantiation -> eltType should not be empty
CHPL_ASSERT(instantiatedFrom_ == nullptr || eltType_ != nullptr);
}
: PtrType(typetags::CPtrType, instantiatedFrom, eltType),
isConst_(isConst) {}

bool contentsMatchInner(const Type* other) const override {
auto rhs = (CPtrType*) other;
auto rhs = (CPtrType*)other;
return instantiatedFrom_ == rhs->instantiatedFrom_ &&
eltType_ == rhs->eltType_ &&
isConst_ == rhs->isConst_;
eltType_ == rhs->eltType_ && isConst_ == rhs->isConst_;
}

void markUniqueStringsInner(Context* context) const override {}

Genericity genericity() const override {
if (!eltType_) return GENERIC;
return eltType_->genericity();
}

static const owned<CPtrType>& getCPtrType(Context* context,
const CPtrType* instantiatedFrom,
const Type* eltType,
bool isConst);

const CPtrType* instantiatedFromCPtrType() const {
// at present, only expecting a single level of instantiated-from.
CHPL_ASSERT(instantiatedFrom_ == nullptr ||
instantiatedFrom_->instantiatedFrom_ == nullptr);
return instantiatedFrom_;
}

bool isEltTypeInstantiationOf(Context* context, const CPtrType* other) const;
const Type* eltType, bool isConst);

public:

static const CPtrType* get(Context* context);
static const CPtrType* get(Context* context, const Type* eltType);
static const CPtrType* getCVoidPtrType(Context* context);
Expand All @@ -82,27 +58,13 @@ class CPtrType final : public Type {
static const ID& getId(Context* context);
static const ID& getConstId(Context* context);

const ID& id(Context* context) const {
const ID& id(Context* context) const override {
return isConst() ? getConstId(context) : getId(context);
}

const Type* eltType() const {
return eltType_;
}

const CPtrType* withoutConst(Context* context) const;

bool isConst() const {
return isConst_;
}

bool isVoidPtr() const {
if (eltType_ == nullptr) return false;
return eltType_->isVoidType();
}

bool isInstantiationOf(Context* context,
const CPtrType* genericType) const;
bool isConst() const { return isConst_; }

virtual void stringify(std::ostream& ss,
chpl::StringifyKind stringKind) const override;
Expand Down
62 changes: 62 additions & 0 deletions frontend/include/chpl/types/HeapBufferType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2021-2024 Hewlett Packard Enterprise Development LP
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef CHPL_TYPES_HEAPBUFFER_TYPE_H
#define CHPL_TYPES_HEAPBUFFER_TYPE_H

#include "chpl/types/PtrType.h"
#include "chpl/types/Type.h"

namespace chpl {
namespace types {

class HeapBufferType final : public PtrType {
private:
HeapBufferType(const HeapBufferType* instantiatedFrom, const Type* eltType)
: PtrType(typetags::HeapBufferType, instantiatedFrom, eltType) {}

bool contentsMatchInner(const Type* other) const override {
auto rhs = (HeapBufferType*)other;
return instantiatedFrom_ == rhs->instantiatedFrom_ &&
eltType_ == rhs->eltType_;
}

void markUniqueStringsInner(Context* context) const override {}

static const owned<HeapBufferType>& getHeapBufferType(
Context* context, const HeapBufferType* instantiatedFrom,
const Type* eltType);

public:
static const HeapBufferType* get(Context* context);
static const HeapBufferType* get(Context* context, const Type* eltType);
static const ID& getId(Context* context);

const ID& id(Context* context) const override {
return getId(context);
}

virtual void stringify(std::ostream& ss,
chpl::StringifyKind stringKind) const override;
};

} // end namespace types
} // end namespace chpl

#endif
80 changes: 80 additions & 0 deletions frontend/include/chpl/types/PtrType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2021-2024 Hewlett Packard Enterprise Development LP
* Other additional copyright holders may be indicated within.
*
* The entirety of this work is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef CHPL_TYPES_PTR_TYPE_H
#define CHPL_TYPES_PTR_TYPE_H

#include "chpl/types/Type.h"
#include "chpl/types/QualifiedType.h"

namespace chpl {
namespace types {

class PtrType : public Type {
protected:
const PtrType* instantiatedFrom_;
const Type* eltType_;

PtrType(typetags::TypeTag tag, const PtrType* instantiatedFrom,
const Type* eltType)
: Type(tag), instantiatedFrom_(instantiatedFrom), eltType_(eltType) {
// not an instantiation -> eltType_ should be empty
CHPL_ASSERT(instantiatedFrom_ != nullptr || eltType_ == nullptr);
// is an instantiation -> eltType should not be empty
CHPL_ASSERT(instantiatedFrom_ == nullptr || eltType_ != nullptr);

// check instantiated only from same type of object
CHPL_ASSERT(instantiatedFrom_ == nullptr ||
instantiatedFrom_->tag() == tag);
}

Genericity genericity() const override {
if (!eltType_) return GENERIC;
return eltType_->genericity();
}

bool isEltTypeInstantiationOf(Context* context, const PtrType* other) const;

const PtrType* instantiatedFromPtrType() const {
// at present, only expecting a single level of instantiated-from.
CHPL_ASSERT(instantiatedFrom_ == nullptr ||
instantiatedFrom_->instantiatedFrom_ == nullptr);
return instantiatedFrom_;
}

public:
virtual const ID& id(Context* context) const = 0;

const Type* eltType() const {
return eltType_;
}

bool isVoidPtr() const {
if (eltType_ == nullptr) return false;
return eltType_->isVoidType();
}

bool isInstantiationOf(Context* context,
const PtrType* genericType) const;
};

} // end namespace types
} // end namespace chpl

#endif
4 changes: 2 additions & 2 deletions frontend/include/chpl/types/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ class Type {
}

/** returns true for a type that is a kind of pointer */
bool isPtrType() const {
return isClassType() || isCFnPtrType() || isCVoidPtrType() || isCPtrType();
bool isAnyPtrType() const {
return isClassType() || isCFnPtrType() || isCVoidPtrType() || isPtrType();
}

/** returns true for a pointer type that can store nil */
Expand Down
2 changes: 2 additions & 0 deletions frontend/include/chpl/types/all-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "chpl/types/ErroneousType.h"
#include "chpl/types/ExternType.h"
#include "chpl/types/FnIteratorType.h"
#include "chpl/types/HeapBufferType.h"
#include "chpl/types/ImagType.h"
#include "chpl/types/IntType.h"
#include "chpl/types/IteratorType.h"
Expand All @@ -42,6 +43,7 @@
#include "chpl/types/NothingType.h"
#include "chpl/types/Param.h"
#include "chpl/types/PrimitiveType.h"
#include "chpl/types/PtrType.h"
#include "chpl/types/PromotionIteratorType.h"
#include "chpl/types/QualifiedType.h"
#include "chpl/types/RealType.h"
Expand Down
14 changes: 8 additions & 6 deletions frontend/include/chpl/types/type-classes-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ TYPE_NODE(NilType)
TYPE_NODE(NothingType)
TYPE_NODE(UnknownType)
TYPE_NODE(VoidType)
TYPE_NODE(CPtrType)

TYPE_BEGIN_SUBCLASSES(PtrType)
TYPE_NODE(CPtrType)
TYPE_NODE(HeapBufferType)
TYPE_END_SUBCLASSES(PtrType)

TYPE_BEGIN_SUBCLASSES(IteratorType)
TYPE_NODE(LoopExprIteratorType)
Expand All @@ -56,11 +60,9 @@ TYPE_END_SUBCLASSES(IteratorType)


// TODO:
// migrate BytesType / StringType to something backed by the modules
// (if the modules are parsed) and also do the same for array, domain,
// distribution.
//
// c_ptr
// migrate array and distribution to something backed by the modules
// (if the modules are parsed)

// c_array

TYPE_BEGIN_SUBCLASSES(BuiltinType)
Expand Down
Loading
Loading