Skip to content

Clang 21: unreachable produced when compiling OpenCL C for NVPTX target #154772

@franz

Description

@franz

When compiling this simple OpenCL kernel:

kernel 
void test_kernel(constant char* input) {
    if (input[0] == 'X') printf("match");
}

Clang 20 with NVPTX target (clang-20 -xcl -fno-jump-tables -Xclang -triple=nvptx64 -Xclang -target-cpu -Xclang sm_50 -emit-llvm) produces the expected code:

; Function Attrs: convergent noinline norecurse nounwind uwtable
define dso_local ptx_kernel void @test_kernel(ptr addrspace(4) nocapture noundef readonly align 1 %0) local_unnamed_addr #0 !kernel_arg_addr_space !7 !kernel_arg_access_qual !8 !kernel_arg_type !9 !kernel_arg_base_type !9 !kernel_arg_type_qual !10 {
  %2 = load i8, ptr addrspace(4) %0, align 1, !tbaa !11
  %3 = icmp eq i8 %2, 88
  br i1 %3, label %4, label %6

4:                                                ; preds = %1
  %5 = tail call i32 (ptr addrspace(4), ...) @printf(ptr addrspace(4) noundef @.str) #2
  br label %6

6:                                                ; preds = %4, %1
  ret void
}

Clang 21 with SPIR64 target (clang-21 -xcl -fno-jump-tables --target=spir64-unknown-unknown -emit-llvm) also works:

; Function Attrs: convergent nofree norecurse nounwind
define dso_local spir_kernel void @test_kernel(ptr addrspace(2) noundef readonly align 1 captures(none) %0) local_unnamed_addr #0 !kernel_arg_addr_space !4 !kernel_arg_access_qual !5 !kernel_arg_type !6 !kernel_arg_base_type !6 !kernel_arg_type_qual !7 {
  %2 = load i8, ptr addrspace(2) %0, align 1, !tbaa !8
  %3 = icmp eq i8 %2, 88
  br i1 %3, label %4, label %6

4:                                                ; preds = %1
  %5 = tail call spir_func i32 (ptr addrspace(2), ...) @printf(ptr addrspace(2) noundef @.str) #3
  br label %6

6:                                                ; preds = %1, %4
  ret void
}

; Function Attrs: alwaysinline convergent nofree norecurse nounwind
define dso_local spir_func void @__clang_ocl_kern_imp_test_kernel(ptr addrspace(2) noundef readonly align 1 captures(none) %0) local_unnamed_addr #1 !kernel_arg_addr_space !4 !kernel_arg_access_qual !5 !kernel_arg_type !6 !kernel_arg_base_type !6 !kernel_arg_type_qual !7 {
  %2 = load i8, ptr addrspace(2) %0, align 1, !tbaa !8
  %3 = icmp eq i8 %2, 88
  br i1 %3, label %4, label %6

4:                                                ; preds = %1
  %5 = tail call spir_func i32 (ptr addrspace(2), ...) @printf(ptr addrspace(2) noundef @.str) #3
  br label %6

6:                                                ; preds = %4, %1
  ret void
}

However, Clang-21 with NVPTX target (clang-21 -xcl -fno-jump-tables -Xclang -triple=nvptx64 -Xclang -target-cpu -Xclang sm_50 -emit-llvm) produces a kernel with a single unreachable instruction:

; Function Attrs: mustprogress nofree noinline norecurse noreturn nosync nounwind willreturn memory(none) uwtable
define dso_local ptx_kernel void @test_kernel(ptr addrspace(4) noundef readnone align 1 captures(none) %0) local_unnamed_addr #0 !kernel_arg_addr_space !7 !kernel_arg_access_qual !8 !kernel_arg_type !9 !kernel_arg_base_type !9 !kernel_arg_type_qual !10 {
  unreachable
}

; Function Attrs: convergent noinline norecurse nounwind uwtable
define dso_local ptx_kernel void @__clang_ocl_kern_imp_test_kernel(ptr addrspace(4) noundef readonly align 1 captures(none) %0) local_unnamed_addr #1 !kernel_arg_addr_space !7 !kernel_arg_access_qual !8 !kernel_arg_type !9 !kernel_arg_base_type !9 !kernel_arg_type_qual !10 {
  %2 = load i8, ptr addrspace(4) %0, align 1, !tbaa !11
  %3 = icmp eq i8 %2, 88
  br i1 %3, label %4, label %6

4:                                                ; preds = %1
  %5 = tail call i32 (ptr addrspace(4), ...) @printf(ptr addrspace(4) noundef @.str) #3
  br label %6

6:                                                ; preds = %4, %1
  ret void
}

Tested with apt.llvm.org Clang version: 21.1.0 (++20250811123159+6f5c887e557f-1~exp1~20250811123320.21)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions