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

C codegen with Target::CPlusPlusMangling generates incorrect type cast #6636

Closed
knzivid opened this issue Mar 4, 2022 · 1 comment
Closed
Assignees

Comments

@knzivid
Copy link
Contributor

knzivid commented Mar 4, 2022

Steps

  1. Build release/13.x halide python bindings
  2. Run the python snippet at the end
  3. Compile the output C file with g++ into an object

Expected: Since TargetFeature.CPlusPlusMangling is enabled, I can compile the generated C source with g++.

Actual: Compile error. I extracted the offending code to https://www.godbolt.org/z/bTbT7PKaT

reprod.c: In function ‘int consumer(halide_buffer_t*)’:
reprod.c:2657:47: error: invalid conversion from ‘const void*’ to ‘void*’ [-fpermissive]
 2657 |  void *const _60 = ((const void * *)_opencl)[0];
      |                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
      |                                               |
      |                                               const void*
reprod.c:2897:49: error: invalid conversion from ‘const void*’ to ‘void*’ [-fpermissive]
 2897 |   void *const _202 = ((const void * *)_opencl)[0];
      |                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
      |                                                 |
      |                                                 const void*

Here are the two offending snippets

 void *const _56 = _halide_buffer_get_host(_opencl_buf_buffer);
 void * _opencl = _56;
 void *const _57 = _halide_buffer_get_host(_opencl_gpu_source_kernels_buffer);
 int32_t const _58 = halide_opencl_initialize_kernels(_ucon, _opencl, _57, 4166);
 bool const _59 = _58 == 0;
 if (!_59)
 {
  return _58;
 }
 void *const _60 = ((const void * *)_opencl)[0]; // error 1
  void *const _202 = ((const void * *)_opencl)[0]; // error 2

def find_gpu_target():
    host_target = get_host_target()
    return Target(host_target.os, host_target.arch, host_target.bits,
                  [TargetFeature.OpenCL, TargetFeature.CLDoubles, TargetFeature.CPlusPlusMangling])

x, y = Var(), Var()
consumer = Func("consumer")
lut_const = np.arange(128)
lut_buffer = Buffer(lut_const)

at_lut = Func("at_lut")
i = Var()
at_lut[i] = lut_buffer[i] % 16
consumer[x, y] = at_lut[(x + y) % lut_const.shape[0]] * (x * x + y)

xo, yo, xi, yi = Var(), Var(), Var(), Var()
consumer.gpu_tile(x, y, xo, yo, xi, yi, 8, 8)

consumer.compile_to(outputs={
    Output.c_source: str(Path(__file__).parent / "reprod.c"),
}, arguments=[], fn_name="consumer", target=find_gpu_target())
@steven-johnson steven-johnson self-assigned this Mar 4, 2022
steven-johnson added a commit that referenced this issue Mar 4, 2022
in the Load handler, we need to emit the cast in the form `TYPE const *` rather than `const TYPE *`, as TYPE could be `void *`, and the const would bind in a way we don't want.
steven-johnson added a commit that referenced this issue Mar 7, 2022
in the Load handler, we need to emit the cast in the form `TYPE const *` rather than `const TYPE *`, as TYPE could be `void *`, and the const would bind in a way we don't want.
@steven-johnson
Copy link
Contributor

fixed by #6638

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

No branches or pull requests

2 participants