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
9 changes: 7 additions & 2 deletions .github/workflows/cmake-test-release-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,15 @@ jobs:
continue-on-error: false
shell: sh
# Public resolvers: the runner's own DNS sometimes fails on the Ubuntu mirrors ("Temporary
# failure resolving 'azure.archive.ubuntu.com'") while every other host resolves.
# failure resolving 'azure.archive.ubuntu.com'") while every other host resolves. Even then a
# lookup can drop for a few seconds, longer than apt's own retries, so the whole step retries.
run: |
printf 'nameserver 8.8.8.8\nnameserver 1.1.1.1\n' | sudo tee /etc/resolv.conf > /dev/null
sudo apt-get -o Acquire::Retries=3 update && sudo apt-get -o Acquire::Retries=3 install -y ninja-build zlib1g-dev libzstd-dev libxml2-dev
for i in 1 2 3 4 5; do
sudo apt-get -o Acquire::Retries=3 update && sudo apt-get -o Acquire::Retries=3 install -y ninja-build zlib1g-dev libzstd-dev libxml2-dev && exit 0
echo "apt-get failed (attempt $i), retrying in 30 s"; sleep 30
done
exit 1

# - name: Install Clang 12
# continue-on-error: false
Expand Down
15 changes: 12 additions & 3 deletions .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,15 @@ jobs:
shell: sh
# zlib, zstd and libxml2: the official LLVM package's CMake targets link against them.
# Public resolvers: the runner's own DNS sometimes fails on the Ubuntu mirrors ("Temporary
# failure resolving 'azure.archive.ubuntu.com'") while every other host resolves.
# failure resolving 'azure.archive.ubuntu.com'") while every other host resolves. Even then a
# lookup can drop for a few seconds, longer than apt's own retries, so the whole step retries.
run: |
printf 'nameserver 8.8.8.8\nnameserver 1.1.1.1\n' | sudo tee /etc/resolv.conf > /dev/null
sudo apt-get -o Acquire::Retries=3 update && sudo apt-get -o Acquire::Retries=3 install -y ninja-build libcurl4-openssl-dev zlib1g-dev libzstd-dev libxml2-dev
for i in 1 2 3 4 5; do
sudo apt-get -o Acquire::Retries=3 update && sudo apt-get -o Acquire::Retries=3 install -y ninja-build libcurl4-openssl-dev zlib1g-dev libzstd-dev libxml2-dev && exit 0
echo "apt-get failed (attempt $i), retrying in 30 s"; sleep 30
done
exit 1

# - name: Install Clang 12
# continue-on-error: false
Expand Down Expand Up @@ -561,7 +566,11 @@ jobs:
# Public resolvers: see "Install Ninja" above.
run: |
printf 'nameserver 8.8.8.8\nnameserver 1.1.1.1\n' | sudo tee /etc/resolv.conf > /dev/null
sudo apt-get -o Acquire::Retries=3 update && sudo apt-get -o Acquire::Retries=3 install -y libcurl4-openssl-dev
for i in 1 2 3 4 5; do
sudo apt-get -o Acquire::Retries=3 update && sudo apt-get -o Acquire::Retries=3 install -y libcurl4-openssl-dev && exit 0
echo "apt-get failed (attempt $i), retrying in 30 s"; sleep 30
done
exit 1

- name: Clone Default Library
shell: bash
Expand Down
86 changes: 84 additions & 2 deletions tslang/include/TypeScript/MLIRLogic/MLIRTypeHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,48 @@ class MLIRTypeHelper
static mlir::Type getMissingValueType(mlir_ts::ValueType valueType)
{
mlir::SmallVector<mlir::Type> members{valueType, mlir_ts::NullType::get(valueType.getContext())};
std::sort(members.begin(), members.end(), [](auto i, auto j) { return i.getAsOpaquePointer() < j.getAsOpaquePointer(); });
sortUnionMembers(members);
return mlir_ts::OptionalType::get(mlir_ts::UnionType::get(valueType.getContext(), members));
}

// The one order of a union's members, so `A | B` and `B | A` are one type. By the members'
// printed text, which is the same in every compiler run: a declaration written out by one run
// (the default library's) has to match the type another run makes of its source. The address
// of a type, used before, differs between runs. Members that print alike are ordered by
// address, as they cannot be told apart in text anyway.
static void sortUnionMembers(mlir::SmallVectorImpl<mlir::Type> &members)
{
if (members.size() < 2)
{
return;
}

mlir::SmallVector<std::pair<std::string, mlir::Type>> keyed;
keyed.reserve(members.size());
for (auto member : members)
{
std::string text;
llvm::raw_string_ostream os(text);
member.print(os);
os.flush();
keyed.emplace_back(std::move(text), member);
}

std::sort(keyed.begin(), keyed.end(), [](const auto &a, const auto &b) {
if (a.first != b.first)
{
return a.first < b.first;
}

return a.second.getAsOpaquePointer() < b.second.getAsOpaquePointer();
});

for (size_t i = 0; i < members.size(); i++)
{
members[i] = keyed[i].second;
}
}

// Where null of a @value type T is in T itself: the index path, into T's storage, of a pointer no
// value of T holds as null - the `this` of a field of an interface type (`{vtable, this}`, null
// exactly when the interface is), or a field of a class type - so `T | null` needs no tag: null is
Expand Down Expand Up @@ -3874,7 +3912,7 @@ class MLIRTypeHelper
newTypes.push_back(type);
}

std::sort (newTypes.begin(), newTypes.end(), [](auto i, auto j) { return (i.getAsOpaquePointer() < j.getAsOpaquePointer()); });
sortUnionMembers(newTypes);

return isUndefined
? mlir::Type(mlir_ts::OptionalType::get(mlir_ts::UnionType::get(context, newTypes)))
Expand Down Expand Up @@ -4145,6 +4183,37 @@ class MLIRTypeHelper
return false;
}

// `valueType` when `literalType` is an object literal (a tuple) with its fields, by name and in
// order, each converting without loss (`{ at: 6 }`'s s32 to Num's number). Kept apart in a union,
// the two have one tag ("object") but not one layout, and a field read from the union takes
// the first member's: right or wrong by the members' order.
mlir_ts::ValueType literalConvertsToValueType(mlir::Type literalType, mlir::Type valueTypeIn)
{
auto valueType = dyn_cast_or_null<mlir_ts::ValueType>(valueTypeIn);
if (!valueType)
{
return mlir_ts::ValueType();
}

auto storage = dyn_cast<mlir_ts::TupleType>(stripValueType(valueType));
auto literal = dyn_cast_or_null<mlir_ts::TupleType>(convertConstTupleTypeToTupleType(literalType));
if (!storage || !literal || storage.getFields().size() != literal.getFields().size())
{
return mlir_ts::ValueType();
}

for (auto [from, to] : llvm::zip(literal.getFields(), storage.getFields()))
{
auto fromType = stripLiteralType(from.type);
if (from.id != to.id || !(fromType == to.type || canWideTypeWithoutDataLoss(fromType, to.type)))
{
return mlir_ts::ValueType();
}
}

return valueType;
}

mlir::Type mergeType(mlir::Location location, mlir::Type existType, mlir::Type currentType, bool& merged)
{
merged = false;
Expand Down Expand Up @@ -4178,6 +4247,19 @@ class MLIRTypeHelper
return currentType;
}

// an object literal of a @value type's fields is that type: `n ?? { at: 6 }` is `Num`
if (auto valueType = literalConvertsToValueType(currentType, existType))
{
merged = true;
return valueType;
}

if (auto valueType = literalConvertsToValueType(existType, currentType))
{
merged = true;
return valueType;
}

// in case of merging integer types with sign/no sign
auto mergedInts = false;
auto resNewIntType = mergeIntTypes(existType, currentType, mergedInts);
Expand Down
5 changes: 5 additions & 0 deletions tslang/test/tester/value/value_missing_ops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ function main() {
const fallback: Mark = { at: 7, owner: new Owner(7) };
assert((mark(-1) ?? fallback).at == 7);
assert((num(-1) ?? num(4)).at == 4 && (num(5) ?? { at: 6 }).at == 5);
// a literal of Num's fields beside a Num is a Num: kept as a union of the two, a field was read
// as the first member's layout, and got 0 when the literal (s32 fields) was ordered first
const five: Num = { at: 5 };
const pick = five.at > 0;
assert((pick ? five : { at: 6 }).at == 5 && (five || { at: 6 }).at == 5, "a literal beside a Num");
const nothing: Mark | undefined = undefined;
assert((nothing ?? fallback).at == 7);

Expand Down
Loading