Skip to content

Commit

Permalink
feat(dyno): implement various primitive call resolution (chapel-lang#…
Browse files Browse the repository at this point in the history
…24022)

This PR implements type resolution on a number of primitives in Dyno.  

The table below shows the different primitives and how production deals
with resolving their return types now.

To note is that these do not test for correctness of actual types or
number of actuals. The tests demonstrate that by making the primitive
calls without additional arguments other than the primitive text.

[reviewed by @mppf, @dlongnecke-cray - thanks!]
  • Loading branch information
arezaii authored Dec 12, 2023
2 parents 34abdea + f45d77d commit 8f36bd1
Show file tree
Hide file tree
Showing 4 changed files with 363 additions and 133 deletions.
74 changes: 44 additions & 30 deletions frontend/lib/resolution/prims.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,6 @@ CallResolutionResult resolvePrimCall(Context* context,
type = primFieldByNum(context, ci);
break;

case PRIM_CLASS_NAME_BY_ID:
case PRIM_ITERATOR_RECORD_FIELD_VALUE_BY_FORMAL:
case PRIM_IS_GENERIC_TYPE:
case PRIM_IS_CLASS_TYPE:
Expand All @@ -725,11 +724,10 @@ CallResolutionResult resolvePrimCall(Context* context,
case PRIM_IS_CONST_COPYABLE:
case PRIM_IS_ASSIGNABLE:
case PRIM_IS_CONST_ASSIGNABLE:
case PRIM_HAS_DEFAULT_VALUE:
case PRIM_NEEDS_AUTO_DESTROY:
case PRIM_HAS_DEFAULT_VALUE: // param uses in module code
case PRIM_NEEDS_AUTO_DESTROY: // param uses in module code
CHPL_UNIMPL("various primitives");
break;

case PRIM_CALL_RESOLVES:
case PRIM_CALL_AND_FN_RESOLVES:
case PRIM_METHOD_CALL_AND_FN_RESOLVES:
Expand Down Expand Up @@ -886,6 +884,7 @@ CallResolutionResult resolvePrimCall(Context* context,
BoolParam::get(context, isEqual));
break;
}
case PRIM_IS_WIDE_PTR:
case PRIM_NOTEQUAL:
case PRIM_LESSOREQUAL:
case PRIM_GREATEROREQUAL:
Expand Down Expand Up @@ -930,12 +929,23 @@ CallResolutionResult resolvePrimCall(Context* context,
case PRIM_POW:
case PRIM_MIN:
case PRIM_MAX:
case PRIM_STEAL:
if (ci.numActuals() > 0) {
type = QualifiedType(QualifiedType::CONST_VAR,
ci.actual(0).type().type());
}
break;

/* primitives that return default int */
case PRIM_GET_UNION_ID:
case PRIM_GET_REQUESTED_SUBLOC:
type = QualifiedType(QualifiedType::CONST_VAR,
IntType::get(context, 0));
break;
/* primitives that return an int32 */
case PRIM_GETCID:
type = QualifiedType(QualifiedType::CONST_VAR,
IntType::get(context, 32));
break;
/* primitives that return void */
case PRIM_NOOP:
case PRIM_MOVE:
Expand Down Expand Up @@ -992,6 +1002,19 @@ CallResolutionResult resolvePrimCall(Context* context,
case PRIM_INVARIANT_START:
case PRIM_GET_TEST_BY_NAME:
case PRIM_GET_TEST_BY_INDEX:
case PRIM_ARRAY_SET:
case PRIM_ARRAY_SET_FIRST:
case PRIM_INIT_FIELDS:
case PRIM_CHPL_COMM_GET:
case PRIM_CHPL_COMM_PUT:
case PRIM_CHPL_COMM_ARRAY_GET:
case PRIM_CHPL_COMM_ARRAY_PUT:
case PRIM_CHPL_COMM_REMOTE_PREFETCH:
case PRIM_CHPL_COMM_GET_STRD:
case PRIM_CHPL_COMM_PUT_STRD:
case PRIM_ARRAY_SHIFT_BASE_POINTER:
case PRIM_AUTO_DESTROY_RUNTIME_TYPE:
case PRIM_CREATE_FN_TYPE:
type = QualifiedType(QualifiedType::CONST_VAR,
VoidType::get(context));
break;
Expand All @@ -1016,11 +1039,20 @@ CallResolutionResult resolvePrimCall(Context* context,
case PRIM_GET_IMAG:
type = primComplexGetComponent(context, ci);
break;
case PRIM_WIDE_GET_ADDR:
type = QualifiedType(QualifiedType::CONST_VAR,
CPtrType::getCVoidPtrType(context));
break;
/* primitives that return a c_string*/
case PRIM_LOOKUP_FILENAME:
case PRIM_CLASS_NAME_BY_ID:
case PRIM_REF_TO_STRING:{
type = QualifiedType(QualifiedType::CONST_VAR,
CStringType::get(context));
break;
}
/* primitives that are not yet handled in dyno */
case PRIM_ACTUALS_LIST:
case PRIM_REF_TO_STRING:
case PRIM_GETCID:
case PRIM_GET_UNION_ID:
case PRIM_GET_MEMBER:
case PRIM_GET_MEMBER_VALUE:
case PRIM_NEW:
Expand Down Expand Up @@ -1057,9 +1089,8 @@ CallResolutionResult resolvePrimCall(Context* context,
case PRIM_GPU_SET_BLOCKSIZE:
case PRIM_ASSERT_ON_GPU:
case PRIM_GPU_ELIGIBLE:
case PRIM_SIZEOF_BUNDLE:
case PRIM_SIZEOF_DDATA_ELEMENT:
case PRIM_INIT_FIELDS:
case PRIM_SIZEOF_BUNDLE: // TODO: this should be sizeType
case PRIM_SIZEOF_DDATA_ELEMENT: // TODO: this should be sizeType
case PRIM_LIFETIME_OF:
CHPL_UNIMPL("misc primitives");
break;
Expand All @@ -1079,17 +1110,7 @@ CallResolutionResult resolvePrimCall(Context* context,
case PRIM_USED_MODULES_LIST:
case PRIM_REFERENCED_MODULES_LIST:
case PRIM_TUPLE_EXPAND:
case PRIM_CHPL_COMM_GET:
case PRIM_CHPL_COMM_PUT:
case PRIM_CHPL_COMM_ARRAY_GET:
case PRIM_CHPL_COMM_ARRAY_PUT:
case PRIM_CHPL_COMM_REMOTE_PREFETCH:
case PRIM_CHPL_COMM_GET_STRD:
case PRIM_CHPL_COMM_PUT_STRD:
case PRIM_ARRAY_GET:
case PRIM_ARRAY_SHIFT_BASE_POINTER:
case PRIM_ARRAY_SET:
case PRIM_ARRAY_SET_FIRST:
case PRIM_MAYBE_LOCAL_THIS:
case PRIM_MAYBE_LOCAL_ARR_ELEM:
case PRIM_MAYBE_AGGREGATE_ASSIGN:
Expand All @@ -1110,17 +1131,13 @@ CallResolutionResult resolvePrimCall(Context* context,
case PRIM_LOGICAL_FOLDER:
case PRIM_WIDE_MAKE:
case PRIM_WIDE_GET_LOCALE:
case PRIM_WIDE_GET_NODE:
case PRIM_WIDE_GET_ADDR:
case PRIM_IS_WIDE_PTR:
case PRIM_WIDE_GET_NODE: // TODO: this should be nodeIdType (int32)
case PRIM_ON_LOCALE_NUM:
case PRIM_GET_REQUESTED_SUBLOC:
case PRIM_REGISTER_GLOBAL_VAR:
case PRIM_BROADCAST_GLOBAL_VARS:
case PRIM_PRIVATE_BROADCAST:
case PRIM_CAPTURE_FN:
case PRIM_CAPTURE_FN_TO_CLASS:
case PRIM_CREATE_FN_TYPE:
case PRIM_GET_USER_LINE:
case PRIM_GET_USER_FILE:
case PRIM_RESOLUTION_POINT:
Expand All @@ -1130,17 +1147,14 @@ CallResolutionResult resolvePrimCall(Context* context,
case PRIM_VIRTUAL_METHOD_CALL:
case PRIM_END_OF_STATEMENT:
case PRIM_CURRENT_ERROR:
case PRIM_STEAL:
case PRIM_AUTO_DESTROY_RUNTIME_TYPE:
case PRIM_GET_RUNTIME_TYPE_FIELD:
case PRIM_LOOKUP_FILENAME:
case PRIM_GET_VISIBLE_SYMBOLS:
case PRIM_STACK_ALLOCATE_CLASS:
case PRIM_ZIP:
case PRIM_NO_ALIAS_SET:
case PRIM_COPIES_NO_ALIAS_SET:
case PRIM_OPTIMIZATION_INFO:
case PRIM_GATHER_TESTS:
case PRIM_GATHER_TESTS: // param uses in module code
CHPL_UNIMPL("misc primitives");
break;

Expand Down
1 change: 1 addition & 0 deletions frontend/test/resolution/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ comp_unit_test(testReflection)
comp_unit_test(testResolve)
comp_unit_test(testResolverVerboseErrors)
comp_unit_test(testReturnTypes)
comp_unit_test(testRuntimePrimitives)
comp_unit_test(testScopeResolve)
comp_unit_test(testScopeTypes)
comp_unit_test(testSplitInit)
Expand Down
103 changes: 0 additions & 103 deletions frontend/test/resolution/testFunctionCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,114 +132,11 @@ static void test3b() {
helpTest3(theFunction);
}

// test that we can handle non-param string for string_length_bytes
static void test4() {
Context ctx;
auto context = &ctx;
QualifiedType qt = resolveTypeOfXInit(context,
R""""(
var s:string;
var x = __primitive("string_length_bytes", s);
)"""");
assert(qt.kind() == QualifiedType::CONST_VAR);
auto typePtr = qt.type();
assert(typePtr);
assert(typePtr->isIntType());
assert(typePtr->toIntType()->bitwidth() == 64);
}

// test that we can handle non-param bytes for string_length_bytes
static void test5() {
Context ctx;
auto context = &ctx;
QualifiedType qt = resolveTypeOfXInit(context,
R""""(
var b:bytes;
var x = __primitive("string_length_bytes", b);
)"""");
assert(qt.kind() == QualifiedType::CONST_VAR);
auto typePtr = qt.type();
assert(typePtr);
assert(typePtr->isIntType());
assert(typePtr->toIntType()->bitwidth() == 64);
}

static void test6() {
// test for primitive return type for get real/imag
Context ctx;
auto context = &ctx;
QualifiedType qt = resolveTypeOfXInit(context,
R""""(
var a: complex(128);
var x = __primitive("complex_get_real", a);
)"""");
assert(qt.kind() == QualifiedType::REF);
auto typePtr = qt.type();
assert(typePtr);
auto realPtr = typePtr->toRealType();
assert(realPtr->bitwidth()==64);
}

static void test7() {
// test for primitive return type for get real/imag
Context ctx;
auto context = &ctx;
QualifiedType qt = resolveTypeOfXInit(context,
R""""(
var a: complex(64);
var x = __primitive("complex_get_real", a);
)"""");
assert(qt.kind() == QualifiedType::REF);
auto typePtr = qt.type();
assert(typePtr);
auto realPtr = typePtr->toRealType();
assert(realPtr->bitwidth()==32);
}

static void test8() {
// test for primitive return type for get real/imag
Context ctx;
auto context = &ctx;
QualifiedType qt = resolveTypeOfXInit(context,
R""""(
var a: complex(128);
var x = __primitive("complex_get_imag", a);
)"""");
assert(qt.kind() == QualifiedType::REF);
auto typePtr = qt.type();
assert(typePtr);
auto realPtr = typePtr->toRealType();
assert(realPtr->bitwidth()==64);
}

static void test9() {
// test for primitive return type for get real/imag
Context ctx;
auto context = &ctx;
QualifiedType qt = resolveTypeOfXInit(context,
R""""(
var a: complex(64);
var x = __primitive("complex_get_imag", a);
)"""");
assert(qt.kind() == QualifiedType::REF);
auto typePtr = qt.type();
assert(typePtr);
auto realPtr = typePtr->toRealType();
assert(realPtr->bitwidth()==32);
}


int main() {
test1();
test2();
test3a();
test3b();
test4();
test5();
test6();
test7();
test8();
test9();

return 0;
}
Loading

0 comments on commit 8f36bd1

Please sign in to comment.