-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[SPIRV] Add Target Builtins using Distance ext as an example #121598
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//===--- BuiltinsSPIRV.td - SPIRV Builtin function database ---------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
include "clang/Basic/BuiltinsBase.td" | ||
|
||
def HLSLDistance : Builtin { | ||
let Spellings = ["__builtin_spirv_distance"]; | ||
let Attributes = [NoThrow, Const]; | ||
let Prototype = "void(...)"; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//===----- SemaSPIRV.h ----- Semantic Analysis for SPIRV constructs--------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// \file | ||
/// This file declares semantic analysis for SPIRV constructs. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_SEMA_SEMASPIRV_H | ||
#define LLVM_CLANG_SEMA_SEMASPIRV_H | ||
|
||
#include "clang/AST/ASTFwd.h" | ||
#include "clang/Sema/SemaBase.h" | ||
|
||
namespace clang { | ||
class SemaSPIRV : public SemaBase { | ||
public: | ||
SemaSPIRV(Sema &S); | ||
|
||
bool CheckSPIRVBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall); | ||
}; | ||
} // namespace clang | ||
|
||
#endif // LLVM_CLANG_SEMA_SEMASPIRV_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//===- SemaSPIRV.cpp - Semantic Analysis for SPIRV constructs--------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// This implements Semantic Analysis for SPIRV constructs. | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "clang/Sema/SemaSPIRV.h" | ||
#include "clang/Basic/TargetBuiltins.h" | ||
#include "clang/Sema/Sema.h" | ||
|
||
namespace clang { | ||
|
||
SemaSPIRV::SemaSPIRV(Sema &S) : SemaBase(S) {} | ||
|
||
bool SemaSPIRV::CheckSPIRVBuiltinFunctionCall(unsigned BuiltinID, | ||
CallExpr *TheCall) { | ||
switch (BuiltinID) { | ||
case SPIRV::BI__builtin_spirv_distance: { | ||
if (SemaRef.checkArgCount(TheCall, 2)) | ||
return true; | ||
|
||
ExprResult A = TheCall->getArg(0); | ||
QualType ArgTyA = A.get()->getType(); | ||
auto *VTyA = ArgTyA->getAs<VectorType>(); | ||
if (VTyA == nullptr) { | ||
SemaRef.Diag(A.get()->getBeginLoc(), | ||
diag::err_typecheck_convert_incompatible) | ||
<< ArgTyA | ||
<< SemaRef.Context.getVectorType(ArgTyA, 2, VectorKind::Generic) << 1 | ||
<< 0 << 0; | ||
return true; | ||
} | ||
|
||
ExprResult B = TheCall->getArg(1); | ||
QualType ArgTyB = B.get()->getType(); | ||
auto *VTyB = ArgTyB->getAs<VectorType>(); | ||
if (VTyB == nullptr) { | ||
SemaRef.Diag(A.get()->getBeginLoc(), | ||
diag::err_typecheck_convert_incompatible) | ||
<< ArgTyB | ||
<< SemaRef.Context.getVectorType(ArgTyB, 2, VectorKind::Generic) << 1 | ||
<< 0 << 0; | ||
return true; | ||
} | ||
|
||
QualType RetTy = VTyA->getElementType(); | ||
TheCall->setType(RetTy); | ||
break; | ||
} | ||
} | ||
return false; | ||
} | ||
} // namespace clang |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5 | ||
|
||
// RUN: %clang_cc1 -O1 -triple spirv-pc-vulkan-compute %s -emit-llvm -o - | FileCheck %s | ||
|
||
typedef float float2 __attribute__((ext_vector_type(2))); | ||
typedef float float3 __attribute__((ext_vector_type(3))); | ||
typedef float float4 __attribute__((ext_vector_type(4))); | ||
|
||
// CHECK-LABEL: define spir_func float @test_distance_float2( | ||
// CHECK-SAME: <2 x float> noundef [[X:%.*]], <2 x float> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] { | ||
// CHECK-NEXT: [[ENTRY:.*:]] | ||
// CHECK-NEXT: [[SPV_DISTANCE:%.*]] = tail call float @llvm.spv.distance.v2f32(<2 x float> [[X]], <2 x float> [[Y]]) | ||
// CHECK-NEXT: ret float [[SPV_DISTANCE]] | ||
// | ||
float test_distance_float2(float2 X, float2 Y) { return __builtin_spirv_distance(X, Y); } | ||
|
||
// CHECK-LABEL: define spir_func float @test_distance_float3( | ||
// CHECK-SAME: <3 x float> noundef [[X:%.*]], <3 x float> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] { | ||
// CHECK-NEXT: [[ENTRY:.*:]] | ||
// CHECK-NEXT: [[SPV_DISTANCE:%.*]] = tail call float @llvm.spv.distance.v3f32(<3 x float> [[X]], <3 x float> [[Y]]) | ||
// CHECK-NEXT: ret float [[SPV_DISTANCE]] | ||
// | ||
float test_distance_float3(float3 X, float3 Y) { return __builtin_spirv_distance(X, Y); } | ||
|
||
// CHECK-LABEL: define spir_func float @test_distance_float4( | ||
// CHECK-SAME: <4 x float> noundef [[X:%.*]], <4 x float> noundef [[Y:%.*]]) local_unnamed_addr #[[ATTR0]] { | ||
// CHECK-NEXT: [[ENTRY:.*:]] | ||
// CHECK-NEXT: [[SPV_DISTANCE:%.*]] = tail call float @llvm.spv.distance.v4f32(<4 x float> [[X]], <4 x float> [[Y]]) | ||
// CHECK-NEXT: ret float [[SPV_DISTANCE]] | ||
// | ||
float test_distance_float4(float4 X, float4 Y) { return __builtin_spirv_distance(X, Y); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// RUN: %clang_cc1 %s -triple spirv-pc-vulkan-compute -verify | ||
|
||
typedef float float2 __attribute__((ext_vector_type(2))); | ||
|
||
float test_no_second_arg(float2 p0) { | ||
return __builtin_spirv_distance(p0); | ||
// expected-error@-1 {{too few arguments to function call, expected 2, have 1}} | ||
} | ||
|
||
float test_too_many_arg(float2 p0) { | ||
return __builtin_spirv_distance(p0, p0, p0); | ||
// expected-error@-1 {{too many arguments to function call, expected 2, have 3}} | ||
} | ||
|
||
float test_double_scalar_inputs(double p0, double p1) { | ||
return __builtin_spirv_distance(p0, p1); | ||
// expected-error@-1 {{passing 'double' to parameter of incompatible type '__attribute__((__vector_size__(2 * sizeof(double)))) double' (vector of 2 'double' values)}} | ||
} | ||
|
||
float test_int_scalar_inputs(int p0, int p1) { | ||
return __builtin_spirv_distance(p0, p1); | ||
// expected-error@-1 {{passing 'int' to parameter of incompatible type '__attribute__((__vector_size__(2 * sizeof(int)))) int' (vector of 2 'int' values)}} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
; RUN: llc -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s | ||
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %} | ||
|
||
; Make sure SPIRV operation function calls for distance are lowered correctly. | ||
|
||
; CHECK-DAG: %[[#op_ext_glsl:]] = OpExtInstImport "GLSL.std.450" | ||
; CHECK-DAG: %[[#float_16:]] = OpTypeFloat 16 | ||
; CHECK-DAG: %[[#vec4_float_16:]] = OpTypeVector %[[#float_16]] 4 | ||
; CHECK-DAG: %[[#float_32:]] = OpTypeFloat 32 | ||
; CHECK-DAG: %[[#vec4_float_32:]] = OpTypeVector %[[#float_32]] 4 | ||
|
||
define noundef half @distance_half4(<4 x half> noundef %a, <4 x half> noundef %b) { | ||
entry: | ||
; CHECK: %[[#]] = OpFunction %[[#float_16]] None %[[#]] | ||
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#vec4_float_16]] | ||
; CHECK: %[[#arg1:]] = OpFunctionParameter %[[#vec4_float_16]] | ||
; CHECK: %[[#]] = OpExtInst %[[#float_16]] %[[#op_ext_glsl]] Distance %[[#arg0]] %[[#arg1]] | ||
%spv.distance = call half @llvm.spv.distance.f16(<4 x half> %a, <4 x half> %b) | ||
ret half %spv.distance | ||
} | ||
|
||
define noundef float @distance_float4(<4 x float> noundef %a, <4 x float> noundef %b) { | ||
entry: | ||
; CHECK: %[[#]] = OpFunction %[[#float_32]] None %[[#]] | ||
; CHECK: %[[#arg0:]] = OpFunctionParameter %[[#vec4_float_32]] | ||
; CHECK: %[[#arg1:]] = OpFunctionParameter %[[#vec4_float_32]] | ||
; CHECK: %[[#]] = OpExtInst %[[#float_32]] %[[#op_ext_glsl]] Distance %[[#arg0]] %[[#arg1]] | ||
%spv.distance = call float @llvm.spv.distance.f32(<4 x float> %a, <4 x float> %b) | ||
ret float %spv.distance | ||
} | ||
|
||
declare half @llvm.spv.distance.f16(<4 x half>, <4 x half>) | ||
declare float @llvm.spv.distance.f32(<4 x float>, <4 x float>) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@farzonl @michalpaszkowski I wonder should we extend this new approach to OpenCL as well?