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

Report error when finding variadic function other than printf #2703

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,16 @@ SPIRVType *LLVMToSPIRVBase::transSPIRVOpaqueType(StringRef STName,
SPIRVType *LLVMToSPIRVBase::transScavengedType(Value *V) {
if (auto *F = dyn_cast<Function>(V)) {
FunctionType *FnTy = Scavenger->getFunctionType(F);
// VarArg functions other than printf are not supported in SPIR-V.
if (FnTy->isVarArg()) {
StringRef DemangledName;
oclIsBuiltin(F->getName(), DemangledName);
BM->getErrorLog().checkError(
(DemangledName.starts_with("printf(") ||
DemangledName.starts_with("__spirv_ocl_printf(")),
Copy link
Contributor

@LU-JOHN LU-JOHN Sep 5, 2024

Choose a reason for hiding this comment

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

oclIsBuiltin demangles "printf" to "__spirv_ocl_printf". I think only checking for: DemangledName.starts_with("__spirv_ocl_printf") should be sufficient. I think we should possibly be able to check for equality instead of starts_with.

Also does DemangledName include an open parenthesis?

oclIsBuiltin starts with:

SPIRVUtil.cpp:bool oclIsBuiltin(StringRef Name, StringRef &DemangledName, bool IsCpp) {
SPIRVUtil.cpp-  if (Name == "printf") {
SPIRVUtil.cpp-    DemangledName = "__spirv_ocl_printf";
SPIRVUtil.cpp-    return true;
SPIRVUtil.cpp-  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have actually realized none of printf variants can reach this piece of code because they are skipped in

if (isBuiltinTransToInst(&F) || isBuiltinTransToExtInst(&F) ||
. More specifically, isBuiltinTransToExtInst(&F) returns true for printf, so it is just skipped and never added to neither Decls nor Defs. Considering this, I think we should be safe by just checking if the function is variadic to report the error at this point. What do you think? Also tagging the rest of approvers to gather their opinions: @svenvh @MrSidims @VyacheslavLevytskyy

SPIRVEC_UnsupportedVarArgFunction);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM!

SPIRVType *RT = transType(FnTy->getReturnType());
std::vector<SPIRVType *> PT;
for (Argument &Arg : F->args()) {
Expand Down
2 changes: 2 additions & 0 deletions lib/SPIRV/libSPIRV/SPIRVErrorEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ _SPIRV_OP(InvalidVersionNumber,
"Invalid Version Number.")
_SPIRV_OP(UnspecifiedMemoryModel, "Unspecified Memory Model.")
_SPIRV_OP(RepeatedMemoryModel, "Expects a single OpMemoryModel instruction.")
_SPIRV_OP(UnsupportedVarArgFunction,
"Variadic functions other than 'printf' are not supported in SPIR-V.")

/* This is the last error code to have a maximum valid value to compare to */
_SPIRV_OP(InternalMaxErrorCode, "Unknown error code")
4 changes: 2 additions & 2 deletions test/DebugInfo/Generic/2009-11-10-CurrentFn.ll
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ target triple = "spir64-unknown-unknown"

define void @bar(i32 %i) nounwind uwtable ssp !dbg !5 {
entry:
tail call void (...) @foo() nounwind, !dbg !14
tail call void @foo() nounwind, !dbg !14
ret void, !dbg !16
}

declare void @foo(...)
declare void @foo()

declare void @llvm.dbg.value(metadata, metadata, metadata) nounwind readnone

Expand Down
14 changes: 7 additions & 7 deletions test/DebugInfo/Generic/multiline.ll
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ target triple = "spir64-unknown-unknown"
; Function Attrs: nounwind uwtable
define void @f2() #0 !dbg !4 {
entry:
call void (...) @f1(), !dbg !11
call void (...) @f1(), !dbg !12
call void (...) @f1(), !dbg !13
call void (...) @f1(), !dbg !14
call void (...) @f1(), !dbg !15
call void (...) @f1(), !dbg !16
call void @f1(), !dbg !11
call void @f1(), !dbg !12
call void @f1(), !dbg !13
call void @f1(), !dbg !14
call void @f1(), !dbg !15
call void @f1(), !dbg !16
ret void, !dbg !17
}

declare void @f1(...) #1
declare void @f1() #1

attributes #0 = { nounwind uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
Expand Down
94 changes: 0 additions & 94 deletions test/DebugInfo/Generic/varargs.ll

This file was deleted.

6 changes: 3 additions & 3 deletions test/DebugInfo/X86/abstract_origin.ll
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ target triple = "spir64-unknown-unknown"
; Function Attrs: alwaysinline nounwind ssp uwtable
define void @g() #0 !dbg !7 {
entry:
tail call void (...) @f() #3, !dbg !10
tail call void @f() #3, !dbg !10
ret void, !dbg !11
}

declare void @f(...)
declare void @f()

; Function Attrs: nounwind ssp uwtable
define void @h() #2 !dbg !12 {
entry:
tail call void (...) @f() #3, !dbg !13
tail call void @f() #3, !dbg !13
ret void, !dbg !15
}

Expand Down
4 changes: 2 additions & 2 deletions test/DebugInfo/X86/dbg-value-const-byref.ll
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ entry:
call void @llvm.dbg.value(metadata i32 3, metadata !10, metadata !DIExpression()), !dbg !15
%call = call i32 @f3(i32 3) #3, !dbg !16
call void @llvm.dbg.value(metadata i32 7, metadata !10, metadata !DIExpression()), !dbg !18
%call1 = call i32 (...) @f1() #3, !dbg !19
%call1 = call i32 @f1() #3, !dbg !19
call void @llvm.dbg.value(metadata i32 %call1, metadata !10, metadata !DIExpression()), !dbg !19
store i32 %call1, ptr %i, align 4, !dbg !19, !tbaa !20
call void @llvm.dbg.value(metadata ptr %i, metadata !10, metadata !DIExpression(DW_OP_deref)), !dbg !24
Expand All @@ -61,7 +61,7 @@ entry:

declare i32 @f3(i32)

declare i32 @f1(...)
declare i32 @f1()

declare void @f2(ptr)

Expand Down
4 changes: 2 additions & 2 deletions test/DebugInfo/X86/single-dbg_value.ll
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ target triple = "spir64-unknown-unknown"
define void @f() #0 !dbg !4 {
entry:
tail call void @h(i32 0) #2, !dbg !14
%call = tail call i32 (...) @g() #2, !dbg !15
%call = tail call i32 @g() #2, !dbg !15
tail call void @llvm.dbg.value(metadata i32 %call, metadata !8, metadata !16), !dbg !17
tail call void @h(i32 %call) #2, !dbg !18
ret void, !dbg !19
}

declare void @h(i32)

declare i32 @g(...)
declare i32 @g()

; Function Attrs: nounwind readnone
declare void @llvm.dbg.value(metadata, metadata, metadata) #1
Expand Down
2 changes: 1 addition & 1 deletion test/negative/unsup_invoke_instr.ll
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ lpad:
ret i32 0
}

declare dso_local i32 @__gxx_personality_v0(...)
declare dso_local i32 @__gxx_personality_v0()

attributes #0 = { norecurse "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "sycl-module-id"="/localdisk2/icl/fadeeval/sycl_workspace/reproducer2/test.cpp" "uniform-work-group-size"="true" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { norecurse nounwind readnone "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
Expand Down
18 changes: 18 additions & 0 deletions test/negative/unsupported_vararg_func.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
; Check whether the translator reports an error for a
; function other than printf with variadic arguments.

; RUN: llvm-as < %s -o %t.bc
; RUN: not llvm-spirv %t.bc 2>&1 | FileCheck %s

target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64-G1"
target triple = "spir64_unknown_unknown"
Copy link
Contributor

Choose a reason for hiding this comment

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

spir64-unknown-unknown


; CHECK: UnsupportedVarArgFunction: Variadic functions other than 'printf' are not supported in SPIR-V.
; Function Attrs: convergent
declare spir_func void @for__issue_diagnostic(i32 noundef, i32 noundef, ...) local_unnamed_addr

define i32 @foo() nounwind {
call spir_func void (i32, i32, ...) @for__issue_diagnostic(i32 noundef 41, i32 noundef 0)
ret i32 0
Copy link
Contributor

Choose a reason for hiding this comment

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

two spaces before ret :)

}

Loading