Skip to content

Commit 56c5b66

Browse files
authored
Merge pull request #6448 from LemonBoy/some-vec-fixes
Some vector fixes
2 parents a1ae3f9 + cbbcf60 commit 56c5b66

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

src/ir.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16715,16 +16715,12 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i
1671516715
}
1671616716
ZigType *dest_float_type = nullptr;
1671716717
uint32_t op1_bits;
16718-
if (instr_is_comptime(op1)) {
16718+
if (instr_is_comptime(op1) && result_type->id != ZigTypeIdVector) {
1671916719
ZigValue *op1_val = ir_resolve_const(ira, op1, UndefOk);
1672016720
if (op1_val == nullptr)
1672116721
return ira->codegen->invalid_inst_gen;
1672216722
if (op1_val->special == ConstValSpecialUndef)
1672316723
return ir_const_undef(ira, source_instr, ira->codegen->builtin_types.entry_bool);
16724-
if (result_type->id == ZigTypeIdVector) {
16725-
ir_add_error(ira, &op1->base, buf_sprintf("compiler bug: TODO: support comptime vector here"));
16726-
return ira->codegen->invalid_inst_gen;
16727-
}
1672816724
bool is_unsigned;
1672916725
if (op1_is_float) {
1673016726
BigInt bigint = {};
@@ -16750,6 +16746,7 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i
1675016746
op1_bits += 1;
1675116747
}
1675216748
} else if (op1_is_float) {
16749+
ir_assert(op1_scalar_type->id == ZigTypeIdFloat, source_instr);
1675316750
dest_float_type = op1_scalar_type;
1675416751
} else {
1675516752
ir_assert(op1_scalar_type->id == ZigTypeIdInt, source_instr);
@@ -16759,16 +16756,12 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i
1675916756
}
1676016757
}
1676116758
uint32_t op2_bits;
16762-
if (instr_is_comptime(op2)) {
16759+
if (instr_is_comptime(op2) && result_type->id != ZigTypeIdVector) {
1676316760
ZigValue *op2_val = ir_resolve_const(ira, op2, UndefOk);
1676416761
if (op2_val == nullptr)
1676516762
return ira->codegen->invalid_inst_gen;
1676616763
if (op2_val->special == ConstValSpecialUndef)
1676716764
return ir_const_undef(ira, source_instr, ira->codegen->builtin_types.entry_bool);
16768-
if (result_type->id == ZigTypeIdVector) {
16769-
ir_add_error(ira, &op2->base, buf_sprintf("compiler bug: TODO: support comptime vector here"));
16770-
return ira->codegen->invalid_inst_gen;
16771-
}
1677216765
bool is_unsigned;
1677316766
if (op2_is_float) {
1677416767
BigInt bigint = {};
@@ -16794,6 +16787,7 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i
1679416787
op2_bits += 1;
1679516788
}
1679616789
} else if (op2_is_float) {
16790+
ir_assert(op2_scalar_type->id == ZigTypeIdFloat, source_instr);
1679716791
dest_float_type = op2_scalar_type;
1679816792
} else {
1679916793
ir_assert(op2_scalar_type->id == ZigTypeIdInt, source_instr);
@@ -21934,7 +21928,17 @@ static IrInstGen *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstSrcElemP
2193421928
return ira->codegen->invalid_inst_gen;
2193521929
}
2193621930
safety_check_on = false;
21931+
} else if (array_type->id == ZigTypeIdVector) {
21932+
uint64_t vector_len = array_type->data.vector.len;
21933+
if (index >= vector_len) {
21934+
ir_add_error_node(ira, elem_ptr_instruction->base.base.source_node,
21935+
buf_sprintf("index %" ZIG_PRI_u64 " outside vector of size %" ZIG_PRI_u64,
21936+
index, vector_len));
21937+
return ira->codegen->invalid_inst_gen;
21938+
}
21939+
safety_check_on = false;
2193721940
}
21941+
2193821942
if (array_type->id == ZigTypeIdVector) {
2193921943
ZigType *elem_type = array_type->data.vector.elem_type;
2194021944
uint32_t host_vec_len = array_type->data.vector.len;

test/compile_errors.zig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ const tests = @import("tests.zig");
22
const std = @import("std");
33

44
pub fn addCases(cases: *tests.CompileErrorContext) void {
5+
cases.add("slice sentinel mismatch",
6+
\\export fn entry() void {
7+
\\ const x = @import("std").meta.Vector(3, f32){ 25, 75, 5, 0 };
8+
\\}
9+
, &[_][]const u8{
10+
"tmp.zig:2:62: error: index 3 outside vector of size 3",
11+
});
12+
513
cases.add("slice sentinel mismatch",
614
\\export fn entry() void {
715
\\ const y: [:1]const u8 = &[_:2]u8{ 1, 2 };
@@ -7548,7 +7556,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
75487556
});
75497557

75507558
cases.add( // fixed bug #2032
7551-
"compile diagnostic string for top level decl type",
7559+
"compile diagnostic string for top level decl type",
75527560
\\export fn entry() void {
75537561
\\ var foo: u32 = @This(){};
75547562
\\}

test/stage1/behavior/vector.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@ test "vector comparison operators" {
274274
expectEqual(@splat(4, true), v1 != v3);
275275
expectEqual(@splat(4, false), v1 != v2);
276276
}
277+
{
278+
// Comptime-known LHS/RHS
279+
var v1: @Vector(4, u32) = [_]u32{ 2, 1, 2, 1 };
280+
const v2 = @splat(4, @as(u32, 2));
281+
const v3: @Vector(4, bool) = [_]bool{ true, false, true, false };
282+
expectEqual(v3, v1 == v2);
283+
expectEqual(v3, v2 == v1);
284+
}
277285
}
278286
};
279287
S.doTheTest();

0 commit comments

Comments
 (0)