Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sema: catch invalid asm input operands #16047

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

wooster0
Copy link
Contributor

@wooster0 wooster0 commented Jun 15, 2023

$ zig-out/bin/zig test test/cases/compile_errors/non_sized_asm_input_operand.zig
test/cases/compile_errors/non_sized_asm_input_operand.zig:4:23: error: input operand of type 'type' is not sized
        : [_] "{al}" (u8),
                      ^~
test/cases/compile_errors/non_sized_asm_input_operand.zig:12:22: error: input operand of type 'type' is not sized
          [_] "{x}" (void),
                     ^~~~
test/cases/compile_errors/non_sized_asm_input_operand.zig:19:22: error: input operand of type 'type' is not sized
        : [_] "{x}" (enum {}),
                     ^~~~~~~

I don't know if disallowing non-sized types is the best solution because for example RISC-V has a zero register that is hardwired to zero so : [x0] "{x0}" (@as(u0, 0)), in a way could be valid but u0 is non-sized so it won't work.
At the same time though you can argue that anything that isn't sized can't actually be in a register.

Fixes #7843 (wasn't an issue with just stage1 btw, stage2 too segfaults without this patch)

src/Sema.zig Outdated Show resolved Hide resolved
src/Sema.zig Outdated Show resolved Hide resolved
@wooster0 wooster0 changed the title Sema: catch non-sized asm input operands Sema: catch invalid asm input operands Jul 11, 2023
@wooster0 wooster0 force-pushed the inpop branch 3 times, most recently from 7c8dac9 to 56d0cf8 Compare July 11, 2023 16:53
src/Sema.zig Outdated Show resolved Hide resolved
test/behavior/asm.zig Outdated Show resolved Hide resolved
@wooster0
Copy link
Contributor Author

updated

@wooster0 wooster0 force-pushed the inpop branch 2 times, most recently from 6e19351 to e4b516a Compare May 13, 2024 16:39
@wooster0
Copy link
Contributor Author

tests pass

@wooster0 wooster0 requested review from mlugg, andrewrk and jacobly0 May 14, 2024 09:21
Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a rebase but after that I think it can be merged. Thanks.

src/Sema.zig Outdated Show resolved Hide resolved
test/behavior/asm.zig Outdated Show resolved Hide resolved
src/Sema.zig Outdated
Comment on lines 17166 to 17173
if (try sema.typeRequiresComptime(uncasted_arg_ty)) {
return sema.fail(
block,
input_op_src,
"type '{}' is comptime-only and cannot be used for an assembly input operand",
.{uncasted_arg_ty.fmt(pt)},
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I seem to recall something about the SPIR-V backend using types as asm inputs... yeah, here we are, #20550 covers it. @Snektron, I'm a little unclear from that proposal -- does this need to work today to avoid regressing stuff? If so, we'll need to add a special case here for SPIR-V.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still a proposal so I assume it's not implemented yet so this can be changed when that proposal gets implemented? Well let's wait for an answer from @Snektron.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SPIR-V is a typed language so regardless of the status of that proposal, We still need to be able to pass types through asm inputs/outputs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So would I do this?

                if (try sema.typeRequiresComptime(uncasted_arg_ty)) {
                    const target = pt.zcu.getTarget();
                    const arch = target.cpu.arch;
                    const is_spirv = arch.isSpirV();
                    if (uncasted_arg_ty.zigTypeTag(zcu) != .Type and !is_spirv) {
                        return sema.fail(
                            block,
                            input_op_src,
                            "type '{}' is comptime-only and cannot be used for an assembly input operand",
                            .{uncasted_arg_ty.fmt(pt)},
                        );
                    }
                }

Copy link
Member

@mlugg mlugg Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/and/or/. Aside from that, looks good.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its not properly implemented in SPIR-V yet in any case, feel free to do whatever for now. We'll make the necessary changes when its required.

Sorry for the late reply 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unreachable at analyze.cpp:9529 in resolve_llvm_types
6 participants