Skip to content

Commit ec906a9

Browse files
committed
fix codegen, update docs
1 parent 1f66435 commit ec906a9

File tree

3 files changed

+17
-43
lines changed

3 files changed

+17
-43
lines changed

doc/langref.html.in

+10-35
Original file line numberDiff line numberDiff line change
@@ -6728,17 +6728,8 @@ async fn func(y: *i32) void {
67286728
This builtin function atomically dereferences a pointer and returns the value.
67296729
</p>
67306730
<p>
6731-
{#syntax#}T{#endsyntax#} must be a pointer type, a {#syntax#}bool{#endsyntax#}, a float,
6732-
an integer whose bit count meets these requirements:
6733-
</p>
6734-
<ul>
6735-
<li>At least 8</li>
6736-
<li>At most the same as usize</li>
6737-
<li>Power of 2</li>
6738-
</ul> or an enum with a valid integer tag type.
6739-
<p>
6740-
TODO right now bool is not accepted. Also I think we could make non powers of 2 work fine, maybe
6741-
we can remove this restriction
6731+
{#syntax#}T{#endsyntax#} must be a {#syntax#}bool{#endsyntax#}, a float,
6732+
an integer or an enum.
67426733
</p>
67436734
{#header_close#}
67446735
{#header_open|@atomicRmw#}
@@ -6747,17 +6738,8 @@ async fn func(y: *i32) void {
67476738
This builtin function atomically modifies memory and then returns the previous value.
67486739
</p>
67496740
<p>
6750-
{#syntax#}T{#endsyntax#} must be a pointer type, a {#syntax#}bool{#endsyntax#},
6751-
or an integer whose bit count meets these requirements:
6752-
</p>
6753-
<ul>
6754-
<li>At least 8</li>
6755-
<li>At most the same as usize</li>
6756-
<li>Power of 2</li>
6757-
</ul>
6758-
<p>
6759-
TODO right now bool is not accepted. Also I think we could make non powers of 2 work fine, maybe
6760-
we can remove this restriction
6741+
{#syntax#}T{#endsyntax#} must be a {#syntax#}bool{#endsyntax#}, a float,
6742+
an integer or an enum.
67616743
</p>
67626744
<p>
67636745
Supported operations:
@@ -6782,17 +6764,8 @@ async fn func(y: *i32) void {
67826764
This builtin function atomically stores a value.
67836765
</p>
67846766
<p>
6785-
{#syntax#}T{#endsyntax#} must be a pointer type, a {#syntax#}bool{#endsyntax#}, a float,
6786-
an integer whose bit count meets these requirements:
6787-
</p>
6788-
<ul>
6789-
<li>At least 8</li>
6790-
<li>At most the same as usize</li>
6791-
<li>Power of 2</li>
6792-
</ul> or an enum with a valid integer tag type.
6793-
<p>
6794-
TODO right now bool is not accepted. Also I think we could make non powers of 2 work fine, maybe
6795-
we can remove this restriction
6767+
{#syntax#}T{#endsyntax#} must be a {#syntax#}bool{#endsyntax#}, a float,
6768+
an integer or an enum.
67966769
</p>
67976770
{#header_close#}
67986771
{#header_open|@bitCast#}
@@ -7108,7 +7081,8 @@ fn cmpxchgStrongButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_v
71087081
more efficiently in machine instructions.
71097082
</p>
71107083
<p>
7111-
{#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("builtin").AtomicOrder{#endsyntax#}.
7084+
{#syntax#}T{#endsyntax#} must be a {#syntax#}bool{#endsyntax#}, a float,
7085+
an integer or an enum.
71127086
</p>
71137087
<p>{#syntax#}@TypeOf(ptr).alignment{#endsyntax#} must be {#syntax#}>= @sizeOf(T).{#endsyntax#}</p>
71147088
{#see_also|Compile Variables|cmpxchgWeak#}
@@ -7136,7 +7110,8 @@ fn cmpxchgWeakButNotAtomic(comptime T: type, ptr: *T, expected_value: T, new_val
71367110
However if you need a stronger guarantee, use {#link|@cmpxchgStrong#}.
71377111
</p>
71387112
<p>
7139-
{#syntax#}AtomicOrder{#endsyntax#} can be found with {#syntax#}@import("builtin").AtomicOrder{#endsyntax#}.
7113+
{#syntax#}T{#endsyntax#} must be a {#syntax#}bool{#endsyntax#}, a float,
7114+
an integer or an enum.
71407115
</p>
71417116
<p>{#syntax#}@TypeOf(ptr).alignment{#endsyntax#} must be {#syntax#}>= @sizeOf(T).{#endsyntax#}</p>
71427117
{#see_also|Compile Variables|cmpxchgStrong#}

src/codegen.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -5229,8 +5229,8 @@ static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutableGen *executable, I
52295229
// operand needs widening and truncating
52305230
ptr_val = LLVMBuildBitCast(g->builder, ptr_val,
52315231
LLVMPointerType(get_llvm_type(g, instruction->actual_type), 0), "");
5232-
cmp_val = LLVMConstZExt(cmp_val, get_llvm_type(g, instruction->actual_type));
5233-
new_val = LLVMConstZExt(new_val, get_llvm_type(g, instruction->actual_type));
5232+
cmp_val = LLVMBuildZExt(g->builder, cmp_val, get_llvm_type(g, instruction->actual_type), "");
5233+
new_val = LLVMBuildZExt(g->builder, new_val, get_llvm_type(g, instruction->actual_type), "");
52345234
}
52355235

52365236
LLVMAtomicOrdering success_order = to_LLVMAtomicOrdering(instruction->success_order);
@@ -5846,7 +5846,7 @@ static LLVMValueRef ir_render_atomic_rmw(CodeGen *g, IrExecutableGen *executable
58465846
// operand needs widening and truncating
58475847
LLVMValueRef casted_ptr = LLVMBuildBitCast(g->builder, ptr,
58485848
LLVMPointerType(get_llvm_type(g, instruction->actual_type), 0), "");
5849-
LLVMValueRef casted_operand = LLVMBuildPtrToInt(g->builder, operand, get_llvm_type(g, instruction->actual_type), "");
5849+
LLVMValueRef casted_operand = LLVMBuildZExt(g->builder, operand, get_llvm_type(g, instruction->actual_type), "");
58505850
LLVMValueRef uncasted_result = ZigLLVMBuildAtomicRMW(g->builder, op, casted_ptr, casted_operand, ordering,
58515851
g->is_single_threaded);
58525852
return LLVMBuildTrunc(g->builder, uncasted_result, get_llvm_type(g, operand_type), "");
@@ -5893,10 +5893,10 @@ static LLVMValueRef ir_render_atomic_store(CodeGen *g, IrExecutableGen *executab
58935893
LLVMValueRef value = ir_llvm_value(g, instruction->value);
58945894

58955895
if (instruction->actual_type != nullptr) {
5896-
// operand needs widening and truncating
5896+
// operand needs widening
58975897
ptr = LLVMBuildBitCast(g->builder, ptr,
58985898
LLVMPointerType(get_llvm_type(g, instruction->actual_type), 0), "");
5899-
value = LLVMConstZExt(value, get_llvm_type(g, instruction->actual_type));
5899+
value = LLVMBuildZExt(g->builder, value, get_llvm_type(g, instruction->actual_type), "");
59005900
}
59015901
LLVMValueRef store_inst = gen_store(g, value, ptr, instruction->ptr->value->type);
59025902
LLVMSetOrdering(store_inst, ordering);

test/stage1/behavior/atomics.zig

+2-3
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,15 @@ fn testAtomicRmwFloat() void {
167167

168168
test "atomics with different types" {
169169
testAtomicsWithType(bool, true, false);
170-
inline for (.{ u1, i5, u33 }) |T| {
170+
inline for (.{ u1, i5, u15 }) |T| {
171171
var x: T = 0;
172172
testAtomicsWithType(T, 0, 1);
173173
}
174174
testAtomicsWithType(u0, 0, 0);
175175
testAtomicsWithType(i0, 0, 0);
176176
}
177177

178-
// a and b souldn't need to be comptime
179-
fn testAtomicsWithType(comptime T: type, comptime a: T, comptime b: T) void {
178+
fn testAtomicsWithType(comptime T: type, a: T, b: T) void {
180179
var x: T = b;
181180
@atomicStore(T, &x, a, .SeqCst);
182181
expect(x == a);

0 commit comments

Comments
 (0)