Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tslang/lib/TypeScript/MLIRGenExpressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,27 @@ namespace mlirgen
EXIT_IF_FAILED_OR_NO_VALUE(result)
auto exprValue = V(result);

if (auto reinterpreted = reinterpretReference(location, typeInfo, exprValue))
{
return reinterpreted;
}

CAST_A(castedValue, location, typeInfo, exprValue, genContext);
return castedValue;
}

// `<Reference<i32>>ref` asserted on a reference to something else reads its bytes as i32s: meant
// (hashing a number's bits), so the layout check an implicit conversion gets (cast) is not made
mlir::Value MLIRGenImpl::reinterpretReference(mlir::Location location, mlir::Type type, mlir::Value value)
{
if (isa<mlir_ts::RefType>(type) && isa<mlir_ts::RefType>(value.getType()) && type != value.getType())
{
return builder.create<mlir_ts::CastOp>(location, type, value);
}

return mlir::Value();
}

ValueOrLogicalResult MLIRGenImpl::mlirGen(AsExpression asExpressionAST, const GenContext &genContext)
{
auto location = loc(asExpressionAST);
Expand All @@ -346,6 +363,11 @@ namespace mlirgen
EXIT_IF_FAILED_OR_NO_VALUE(result)
auto exprValue = V(result);

if (auto reinterpreted = reinterpretReference(location, typeInfo, exprValue))
{
return reinterpreted;
}

CAST_A(castedValue, location, typeInfo, exprValue, genContext);
return castedValue;
}
Expand Down
2 changes: 2 additions & 0 deletions tslang/lib/TypeScript/MLIRGenImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4053,6 +4053,8 @@ class MLIRGenImpl

ValueOrLogicalResult mlirGen(TypeAssertion typeAssertionAST, const GenContext &genContext);

mlir::Value reinterpretReference(mlir::Location location, mlir::Type type, mlir::Value value);

ValueOrLogicalResult mlirGen(AsExpression asExpressionAST, const GenContext &genContext);

ValueOrLogicalResult mlirGen(ComputedPropertyName computedPropertyNameAST, const GenContext &genContext);
Expand Down
8 changes: 8 additions & 0 deletions tslang/test/tester/tests/00tslang_issues_24_26.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,13 @@ function main() {
const ints: int[] = [1, 2, 3];
assert(fillF32(Ref(floats[0])) == 1 && fillInt(Ref(ints[1])) == 2, "references laid out alike");

// asserted, a reference reads what it refers to as another type: the default library hashes a
// number's bits so. 1.5 is 0x3FF8000000000000
let d: number = 1.5;
const halves = <Reference<i32>>Ref(d);
assert(Deref(halves[0]) == 0 && Deref(halves[1]) == 0x3ff80000, "<Reference<i32>> of a number");
const low = Ref(d) as Reference<i32>;
assert(Deref(low) == 0, "as Reference<i32>");

print("done.");
}
Loading