Skip to content

Commit a31a603

Browse files
committed
Handle type_value instruction in CSE
1 parent 904aac7 commit a31a603

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

lib/SIL/IR/SILInstruction.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,9 @@ namespace {
897897
return X->getElementType() == RHS->getElementType();
898898
}
899899

900+
bool visitTypeValueInst(const TypeValueInst *RHS) {
901+
return true;
902+
}
900903
private:
901904
const SILInstruction *LHS;
902905
};

lib/SILOptimizer/Transforms/CSE.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,13 @@ class HashVisitor : public SILInstructionVisitor<HashVisitor, llvm::hash_code> {
518518
llvm::hash_combine_range(Operands.begin(), Operands.end()),
519519
X->getElementType());
520520
}
521+
522+
hash_code visitTypeValueInst(TypeValueInst *X) {
523+
OperandValueArrayRef Operands(X->getAllOperands());
524+
return llvm::hash_combine(
525+
X->getKind(), X->getType(),
526+
llvm::hash_combine_range(Operands.begin(), Operands.end()));
527+
}
521528
};
522529
} // end anonymous namespace
523530

@@ -1231,6 +1238,7 @@ bool CSE::canHandle(SILInstruction *Inst) {
12311238
case SILInstructionKind::ScalarPackIndexInst:
12321239
case SILInstructionKind::DynamicPackIndexInst:
12331240
case SILInstructionKind::TuplePackElementAddrInst:
1241+
case SILInstructionKind::TypeValueInst:
12341242
// Intentionally we don't handle (prev_)dynamic_function_ref.
12351243
// They change at runtime.
12361244
#define LOADABLE_REF_STORAGE(Name, ...) \

test/SILOptimizer/cse_ossa.sil

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-sil-opt -sil-print-types -enable-sil-verify-all %s -update-borrowed-from -cse | %FileCheck %s
1+
// RUN: %target-sil-opt -sil-print-types -enable-sil-verify-all %s -update-borrowed-from -cse -enable-experimental-feature ValueGenerics | %FileCheck %s
22
sil_stage canonical
33

44
import Builtin
@@ -1328,3 +1328,25 @@ exit(%proj : @guaranteed $C, %borrow : @guaranteed $S, %owned_s_2 : @owned $S):
13281328
return %copy : $C
13291329
}
13301330

1331+
// CHECK-LABEL: sil [ossa] @type_value_test : {{.*}} {
1332+
// CHECK: type_value
1333+
// CHECK-NOT: type_value
1334+
// CHECK-LABEL: } // end sil function 'type_value_test'
1335+
sil [ossa] @type_value_test : $@convention(thin) <let N : Int> (@in_guaranteed Slab<N, Int>, Int) -> () {
1336+
bb0(%0 : $*Slab<N, Int>, %1 : $Int):
1337+
%2 = integer_literal $Builtin.Int64, 0
1338+
%3 = type_value $Int for N
1339+
%4 = struct_extract %1, #Int._value
1340+
%5 = struct_extract %3, #Int._value
1341+
%6 = builtin "cmp_slt_Int64"(%4, %2) : $Builtin.Int1
1342+
cond_fail %6, "Index out of bounds"
1343+
%8 = integer_literal $Builtin.Int64, 0
1344+
%9 = type_value $Int for N
1345+
%10 = struct_extract %1, #Int._value
1346+
%11 = struct_extract %9, #Int._value
1347+
%12 = builtin "cmp_slt_Int64"(%10, %8) : $Builtin.Int1
1348+
cond_fail %12, "Index out of bounds"
1349+
%14 = tuple ()
1350+
return %14
1351+
}
1352+

0 commit comments

Comments
 (0)