[clang] [llvm] [HLSL] Implement `rsqrt` intrinsic (PR #84820)

2024-03-14 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy approved this pull request.


https://github.com/llvm/llvm-project/pull/84820
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DXIL] Add Float `Dot` Intrinsic Lowering (PR #86071)

2024-03-25 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -295,6 +295,15 @@ def IMad : DXILOpMapping<48, tertiary, int_dx_imad,
  "Signed integer arithmetic multiply/add operation. 
imad(m,a,b) = m * a + b.">;
 def UMad : DXILOpMapping<49, tertiary, int_dx_umad,
  "Unsigned integer arithmetic multiply/add operation. 
umad(m,a,b) = m * a + b.">;
+def Dot2 : DXILOpMapping<54, dot2, int_dx_dot2,
+ "dot product of two float vectors Dot(a,b) = 
a[0]*b[0] + ... + a[n]*b[n] where n is between 0 and 1",
+ 
[llvm_halforfloat_ty,LLVMMatchType<0>,LLVMMatchType<0>,LLVMMatchType<0>,LLVMMatchType<0>]>;
+def Dot3 : DXILOpMapping<55, dot3, int_dx_dot3,
+ "dot product of two float vectors Dot(a,b) = 
a[0]*b[0] + ... + a[n]*b[n] where n is between 0 and 2",
+ 
[llvm_halforfloat_ty,LLVMMatchType<0>,LLVMMatchType<0>,LLVMMatchType<0>,LLVMMatchType<0>,LLVMMatchType<0>,LLVMMatchType<0>]>;
+def Dot4 : DXILOpMapping<56, dot4, int_dx_dot4,
+ "dot product of two float vectors Dot(a,b) = 
a[0]*b[0] + ... + a[n]*b[n] where n is between 0 and 3",
+ 
[llvm_halforfloat_ty,LLVMMatchType<0>,LLVMMatchType<0>,LLVMMatchType<0>,LLVMMatchType<0>,LLVMMatchType<0>,LLVMMatchType<0>,LLVMMatchType<0>,LLVMMatchType<0>]>;

bharadwajy wrote:

```suggestion
let OpTypes = !listconcat([llvm_halforfloat_ty], 
!listsplat(llvm_halforfloat_ty, 4)) in
  def Dot2 : DXILOpMapping<54, dot2, int_dx_dot2,
   "dot product of two float vectors Dot(a,b) = 
a[0]*b[0] + ... + a[n]*b[n] where n is between 0 and 1">;

let OpTypes = !listconcat([llvm_halforfloat_ty], 
!listsplat(llvm_halforfloat_ty, 6)) in
  def Dot3 : DXILOpMapping<55, dot3, int_dx_dot3,
   "dot product of two float vectors Dot(a,b) = 
a[0]*b[0] + ... + a[n]*b[n] where n is between 0 and 2">;
let OpTypes = !listconcat([llvm_halforfloat_ty], 
!listsplat(llvm_halforfloat_ty, 8)) in
  def Dot4 : DXILOpMapping<56, dot4, int_dx_dot4,
   "dot product of two float vectors Dot(a,b) = 
a[0]*b[0] + ... + a[n]*b[n] where n is between 0 and 3">;
```

https://github.com/llvm/llvm-project/pull/86071
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DXIL] Add Float `Dot` Intrinsic Lowering (PR #86071)

2024-03-25 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy approved this pull request.


https://github.com/llvm/llvm-project/pull/86071
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL][DXIL] HLSL's `round` should follow `roundeven` behavior (PR #87078)

2024-03-29 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy approved this pull request.


https://github.com/llvm/llvm-project/pull/87078
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [NFC][HLSL][DX] Update invalid environment tests (PR #81052)

2024-02-07 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy approved this pull request.

Works! LGTM!

https://github.com/llvm/llvm-project/pull/81052
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL][docs] Document hlsl.h in the HLSL docs (PR #84081)

2024-03-05 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy approved this pull request.

Consider consistent spelling of the term "built in" consistently - whichever is 
preferred.

https://github.com/llvm/llvm-project/pull/84081
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL][docs] Document hlsl.h in the HLSL docs (PR #84081)

2024-03-05 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -114,6 +114,44 @@ not re-targetable, we want to share the Clang CodeGen 
implementation for HLSL
 with other GPU graphics targets like SPIR-V and possibly other GPU and even CPU
 targets.
 
+hlsl.h
+--
+
+HLSL has an extensive library of functionality. This is similar to OpenCL and
+CUDA. The implementation approach for the HLSL library functionality draws from
+patterns in use by OpenCL and other Clang resource headers.
+
+Similar to OpenCL, the HLSL library functionality is implicitly declared in
+translation units without needing to include a header to provide declarations.
+In Clang this is handled by making ``hlsl.h`` an implicitly included header
+distributed as part of the Clang resource directory.
+
+Similar to OpenCL, HLSL's implicit header will explicitly declare all 
overloads,
+and each overload will map to a corresponding ``__builtin_*`` compiler 
intrinsic
+that is handled in ClangCodeGen. CUDA uses a similar pattern although many CUDA
+functions have full definitions in the included headers which in turn call
+corresponding ``__builtin*`` compiler intrinsics. By not having bodies HLSL
+avoids the need for the inliner to clean up and inline large numbers of small
+library functions.
+
+HLSL's implicit headers also define some of HLSL's typedefs. This is consistent
+with how the AVX vector header is implemented.
+
+Concerns have been expressed that this approach may result in slower compile
+times than the approach DXC uses where library functions are treated more like
+Clang ``__builtin*`` intrinsics. No real world use cases have been identified
+where parsing is a significant compile-time overhead, but the HLSL implicit
+headers can be compiled into a module for performance if needed.
+
+Further, by treating these functions as functions rather than ``__builtin*``

bharadwajy wrote:

```suggestion
Further, by treating these as traditional language functions rather than as 
``__builtin*`` 
```

https://github.com/llvm/llvm-project/pull/84081
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL][docs] Document hlsl.h in the HLSL docs (PR #84081)

2024-03-05 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -114,6 +114,44 @@ not re-targetable, we want to share the Clang CodeGen 
implementation for HLSL
 with other GPU graphics targets like SPIR-V and possibly other GPU and even CPU
 targets.
 
+hlsl.h
+--
+
+HLSL has an extensive library of functionality. This is similar to OpenCL and
+CUDA. The implementation approach for the HLSL library functionality draws from
+patterns in use by OpenCL and other Clang resource headers.
+
+Similar to OpenCL, the HLSL library functionality is implicitly declared in
+translation units without needing to include a header to provide declarations.
+In Clang this is handled by making ``hlsl.h`` an implicitly included header
+distributed as part of the Clang resource directory.
+
+Similar to OpenCL, HLSL's implicit header will explicitly declare all 
overloads,
+and each overload will map to a corresponding ``__builtin_*`` compiler 
intrinsic
+that is handled in ClangCodeGen. CUDA uses a similar pattern although many CUDA
+functions have full definitions in the included headers which in turn call
+corresponding ``__builtin*`` compiler intrinsics. By not having bodies HLSL
+avoids the need for the inliner to clean up and inline large numbers of small
+library functions.
+
+HLSL's implicit headers also define some of HLSL's typedefs. This is consistent
+with how the AVX vector header is implemented.
+
+Concerns have been expressed that this approach may result in slower compile
+times than the approach DXC uses where library functions are treated more like
+Clang ``__builtin*`` intrinsics. No real world use cases have been identified
+where parsing is a significant compile-time overhead, but the HLSL implicit
+headers can be compiled into a module for performance if needed.
+
+Further, by treating these functions as functions rather than ``__builtin*``
+compiler intrinsics the language behaviors are more consistent and aligned with

bharadwajy wrote:

```suggestion
compiler intrinsics, the language behaviors are more consistent and aligned with
```

https://github.com/llvm/llvm-project/pull/84081
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL][docs] Document hlsl.h in the HLSL docs (PR #84081)

2024-03-05 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -114,6 +114,44 @@ not re-targetable, we want to share the Clang CodeGen 
implementation for HLSL
 with other GPU graphics targets like SPIR-V and possibly other GPU and even CPU
 targets.
 
+hlsl.h
+--
+
+HLSL has an extensive library of functionality. This is similar to OpenCL and

bharadwajy wrote:

```suggestion
HLSL has a library that supports extensive set of functionalities. This is 
similar to OpenCL and
```

https://github.com/llvm/llvm-project/pull/84081
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL] Implementation of the frac intrinsic (PR #83315)

2024-02-29 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -5246,6 +5246,23 @@ bool CheckVectorElementCallArgs(Sema *S, CallExpr 
*TheCall) {
   return true;
 }
 
+bool CheckAllArgsAreFloatRepresentation(Sema *S, CallExpr *TheCall) {

bharadwajy wrote:

Consider changing the function name to either
`CheckAllArgsAreFloatType`, `CheckAllArgsAreFloats` or 
`CheckAllArgsHaveFloatRepresentation`

https://github.com/llvm/llvm-project/pull/83315
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL][DXIL] Implementation of round intrinsic (PR #83570)

2024-03-01 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy approved this pull request.

A few simple suggestions. Otherwise looks good.

https://github.com/llvm/llvm-project/pull/83570
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL][DXIL] Implementation of round intrinsic (PR #83570)

2024-03-01 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy edited 
https://github.com/llvm/llvm-project/pull/83570
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL][DXIL] Implementation of round intrinsic (PR #83570)

2024-03-01 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -0,0 +1,43 @@
+; RUN: opt -S -dxil-op-lower < %s | FileCheck %s
+
+; Make sure dxil operation function calls for round are generated for float 
and half.
+; CHECK:call float @dx.op.unary.f32(i32 26, float %{{.*}})
+; CHECK:call half @dx.op.unary.f16(i32 26, half %{{.*}})
+
+target datalayout = 
"e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
+target triple = "dxil-pc-shadermodel6.7-library"
+
+; Function Attrs: noinline nounwind optnone
+define noundef float @_Z3foof(float noundef %a) #0 {
+entry:
+  %a.addr = alloca float, align 4
+  store float %a, ptr %a.addr, align 4
+  %0 = load float, ptr %a.addr, align 4
+  %elt.round = call float @llvm.round.f32(float %0)
+  ret float %elt.round
+}
+
+; Function Attrs: nocallback nofree nosync nounwind readnone speculatable 
willreturn
+declare float @llvm.round.f32(float) #1
+
+; Function Attrs: noinline nounwind optnone
+define noundef half @_Z3barDh(half noundef %a) #0 {
+entry:
+  %a.addr = alloca half, align 2
+  store half %a, ptr %a.addr, align 2
+  %0 = load half, ptr %a.addr, align 2
+  %elt.round = call half @llvm.round.f16(half %0)
+  ret half %elt.round
+}
+
+; Function Attrs: nocallback nofree nosync nounwind readnone speculatable 
willreturn
+declare half @llvm.round.f16(half) #1
+
+attributes #0 = { noinline nounwind optnone "frame-pointer"="none" 
"min-legal-vector-width"="0" "no-trapping-math"="true" 
"stack-protector-buffer-size"="8" }
+attributes #1 = { nocallback nofree nosync nounwind readnone speculatable 
willreturn }
+
+!llvm.module.flags = !{!0}
+!llvm.ident = !{!1}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project.git 
73417c517644db5c419c85c0b3cb6750172fcab5)"}

bharadwajy wrote:

Add a new line.

https://github.com/llvm/llvm-project/pull/83570
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL][DXIL] Implementation of round intrinsic (PR #83570)

2024-03-01 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -831,6 +831,40 @@ uint64_t3 reversebits(uint64_t3);
 _HLSL_BUILTIN_ALIAS(__builtin_elementwise_bitreverse)
 uint64_t4 reversebits(uint64_t4);
 
+//===--===//
+// frac builtins
+//===--===//
+
+/// \fn T round(T x)
+/// \brief Rounds the specified value to the nearest integer. Halfway cases are
+/// rounded to the nearest even. \a x parameter. \param x The specified input

bharadwajy wrote:

```suggestion
/// rounded to the nearest even value.
/// \param x The specified input
```

Typically `\param` command starts on a new line.

https://github.com/llvm/llvm-project/pull/83570
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL][DXIL] Implementation of round intrinsic (PR #83570)

2024-03-01 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -220,6 +220,9 @@ def Sin  : DXILOpMapping<13, unary, int_sin,
  "Returns sine(theta) for theta in radians.">;
 def UMax : DXILOpMapping<39, binary, int_umax,
  "Unsigned integer maximum. UMax(a,b) = a > b ? a : 
b">;
+def Round : DXILOpMapping<26, unary, int_round,

bharadwajy wrote:

Please consider adding this before the `def UMax` to maintain a sorted order of 
opcode.

https://github.com/llvm/llvm-project/pull/83570
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL][DXIL] Implementation of round intrinsic (PR #83570)

2024-03-01 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -0,0 +1,43 @@
+; RUN: opt -S -dxil-op-lower < %s | FileCheck %s
+
+; Make sure dxil operation function calls for round are generated for float 
and half.
+; CHECK:call float @dx.op.unary.f32(i32 26, float %{{.*}})
+; CHECK:call half @dx.op.unary.f16(i32 26, half %{{.*}})
+
+target datalayout = 
"e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
+target triple = "dxil-pc-shadermodel6.7-library"
+
+; Function Attrs: noinline nounwind optnone
+define noundef float @_Z3foof(float noundef %a) #0 {
+entry:
+  %a.addr = alloca float, align 4
+  store float %a, ptr %a.addr, align 4
+  %0 = load float, ptr %a.addr, align 4
+  %elt.round = call float @llvm.round.f32(float %0)
+  ret float %elt.round
+}
+
+; Function Attrs: nocallback nofree nosync nounwind readnone speculatable 
willreturn
+declare float @llvm.round.f32(float) #1
+
+; Function Attrs: noinline nounwind optnone
+define noundef half @_Z3barDh(half noundef %a) #0 {

bharadwajy wrote:

Consider using unmangled function name - which is `bar` here, but suggest 
something like `round_double` for better readability.

https://github.com/llvm/llvm-project/pull/83570
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL][DXIL] Implementation of round intrinsic (PR #83570)

2024-03-01 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -0,0 +1,43 @@
+; RUN: opt -S -dxil-op-lower < %s | FileCheck %s
+
+; Make sure dxil operation function calls for round are generated for float 
and half.
+; CHECK:call float @dx.op.unary.f32(i32 26, float %{{.*}})
+; CHECK:call half @dx.op.unary.f16(i32 26, half %{{.*}})
+
+target datalayout = 
"e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
+target triple = "dxil-pc-shadermodel6.7-library"
+
+; Function Attrs: noinline nounwind optnone
+define noundef float @_Z3foof(float noundef %a) #0 {

bharadwajy wrote:

Consider using unmangled function name - which is `foo` here, but suggest 
something like `round_float` for better readability.

https://github.com/llvm/llvm-project/pull/83570
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL][DXIL] Implementation of round intrinsic (PR #83570)

2024-03-01 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -0,0 +1,43 @@
+; RUN: opt -S -dxil-op-lower < %s | FileCheck %s
+
+; Make sure dxil operation function calls for round are generated for float 
and half.
+; CHECK:call float @dx.op.unary.f32(i32 26, float %{{.*}})
+; CHECK:call half @dx.op.unary.f16(i32 26, half %{{.*}})
+
+target datalayout = 
"e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
+target triple = "dxil-pc-shadermodel6.7-library"
+
+; Function Attrs: noinline nounwind optnone
+define noundef float @_Z3foof(float noundef %a) #0 {
+entry:
+  %a.addr = alloca float, align 4
+  store float %a, ptr %a.addr, align 4
+  %0 = load float, ptr %a.addr, align 4
+  %elt.round = call float @llvm.round.f32(float %0)
+  ret float %elt.round
+}
+
+; Function Attrs: nocallback nofree nosync nounwind readnone speculatable 
willreturn
+declare float @llvm.round.f32(float) #1
+
+; Function Attrs: noinline nounwind optnone
+define noundef half @_Z3barDh(half noundef %a) #0 {
+entry:
+  %a.addr = alloca half, align 2
+  store half %a, ptr %a.addr, align 2
+  %0 = load half, ptr %a.addr, align 2
+  %elt.round = call half @llvm.round.f16(half %0)
+  ret half %elt.round
+}
+
+; Function Attrs: nocallback nofree nosync nounwind readnone speculatable 
willreturn
+declare half @llvm.round.f16(half) #1
+
+attributes #0 = { noinline nounwind optnone "frame-pointer"="none" 
"min-legal-vector-width"="0" "no-trapping-math"="true" 
"stack-protector-buffer-size"="8" }
+attributes #1 = { nocallback nofree nosync nounwind readnone speculatable 
willreturn }
+
+!llvm.module.flags = !{!0}

bharadwajy wrote:

I think all the lines from here to the end are extraneous and can (should) be 
deleted.

https://github.com/llvm/llvm-project/pull/83570
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL] implement `mad` intrinsic (PR #83826)

2024-03-04 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -0,0 +1,67 @@
+; RUN: opt -S -dxil-op-lower < %s | FileCheck %s
+
+; Make sure dxil operation function calls for round are generated for float 
and half.
+; CHECK:call half @dx.op.tertiary.f16(i32 46, half %{{.*}}, half %{{.*}}, half 
%{{.*}})
+; CHECK:call float @dx.op.tertiary.f32(i32 46, float %{{.*}}, float %{{.*}}, 
float %{{.*}})
+; CHECK:call double @dx.op.tertiary.f64(i32 46, double %{{.*}}, double 
%{{.*}}, double %{{.*}})
+
+
+target datalayout = 
"e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
+target triple = "dxil-pc-shadermodel6.7-library"
+
+; Function Attrs: noinline nounwind optnone
+define noundef half @"?test_mad_half@@YA$f16@$f16@00@Z"(half noundef %p0, half 
noundef %p1, half noundef %p2) #0 {

bharadwajy wrote:

Consider using unmangled names for function names in this as well as other test 
sources of this PR.

https://github.com/llvm/llvm-project/pull/83826
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL] implement `mad` intrinsic (PR #83826)

2024-03-04 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy approved this pull request.


https://github.com/llvm/llvm-project/pull/83826
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL] implement `mad` intrinsic (PR #83826)

2024-03-04 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy edited 
https://github.com/llvm/llvm-project/pull/83826
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL][Doc] Add doc about expected differences (PR #82395)

2024-02-20 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -0,0 +1,107 @@
+
+Expected Differences vs DXC and FXC
+===
+
+.. contents::
+   :local:
+
+Introduction
+
+
+HLSL currently has two reference compilers, the `DirectX Shader Compiler (DXC)
+`_ and the
+`Effect-Compiler (FXC) 
`_.
+The two reference compilers do not fully agree. Some known disagreements in the
+references are tracked on
+`DXC's GitHub
+`_,
+but many more are known to exist.
+
+HLSL as implemented by Clang will also not fully match either of the reference
+implementations, it is instead being written to match the `draft language
+specification `_.
+
+This document is a non-exhaustive collection the known differences between
+Clang's implementation of HLSL and the existing reference compilers.
+
+General Principles
+--
+
+Most of the intended differences between Clang and the earlier reference
+compilers are focused on increased consistency and correctness. Both reference
+compilers do not always apply language rules the same in all contexts.
+
+Clang also deviates from the reference compilers by providing different
+diagnostics, both in terms of the textual messages and the contexts in which
+diagnostics are produced. While striving for a high level of source
+compatibility with conforming HLSL code, Clang may produce earlier and more
+robust diagnostics for incorrect code or reject code that a reference compiler
+incorrectly accepted.
+
+Language Version
+
+
+Clang targets language compatibility for HLSL 2021 as implemented by DXC.
+Language features that were removed in earlier versions of HLSL may be added on
+a case-by-case basis, but are not planned for the initial implementation.
+
+Overload Resolution
+===
+
+Clang's HLSL implementation adopts C++ overload resolution rules as proposed 
for
+HLSL 202x based on proposal
+`0007 
`_
+and
+`0008 
`_.
+
+Clang's implementation extends standard overload resolution rules to HLSL
+library functionality. This causes subtle changes in overload resolution
+behavior between Clang and DXC. Some examples include:
+
+.. code-block:: c++
+
+  void halfOrInt16(half H);
+  void halfOrInt16(uint16_t I);
+
+  void takesDoubles(double, double, double);
+
+  cbuffer CB {
+uint U;
+int I;
+float X, Y, Z;
+double3 A, B;
+  }
+
+  export void call() {
+halfOrInt16(U); // All: Resolves to halfOrInt16(uint16_t).
+halfOrInt16(I); // All: Resolves to halfOrInt16(uint16_t).
+half H;
+  #ifndef IGNORE_ERRORS
+H = asfloat16(I); // DXC: Fails to resolve overload for int.
+  // Clang: Resolves to asfloat16(uint16_t).
+H = asfloat16(U); // DXC: Fails to resolve overload for int.
+  // Clang: Resolves to asfloat16(uint16_t).
+  #endif
+H = asfloat16(0x01); // DXC: Resolves to asfloat16(half).
+ // Clang: Resolves to asfloat16(uint16_t).
+
+takesDoubles(X, Y, Z); // Works on all compilers
+  #ifndef IGNORE_ERRORS
+fma(X, Y, Z); // DXC: Fails to resolve no known conversion from float to 
double.
+  // Clang: Resolces to fma(double,double,double).
+  #endif
+
+double D = dot(A, B); // DXC: Resolves to dot(double3, double3), fails 
DXIL Validation.
+  // FXC: Expands to compute double dot product with 
fmul/fadd
+  // Clang: Resolves to dot(float3, float3), emits 
conversion warnings.
+
+  }
+
+.. note::
+
+  In Clang a conscious decision was made to exclude the ``dot(vector,
+  vector)`` overload and allow overload resolution to resolve the

bharadwajy wrote:

```suggestion
  In Clang a conscious decision was made to exclude the ``dot(vector, 
vector)`` 
  overload and allow overload resolution to resolve the
```
 
The new-line splitting the verbatim text is rendered as such in the rendered 
version. Suggest a fix as above.

https://github.com/llvm/llvm-project/pull/82395
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL][Doc] Add doc about expected differences (PR #82395)

2024-02-20 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy edited 
https://github.com/llvm/llvm-project/pull/82395
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL][Doc] Add doc about expected differences (PR #82395)

2024-02-21 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy approved this pull request.


https://github.com/llvm/llvm-project/pull/82395
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Implement support for HLSL intrinsic - select (PR #107129)

2024-09-03 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -1545,6 +1605,30 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned 
BuiltinID, CallExpr *TheCall) {
 break;
   }
   case Builtin::BI__builtin_hlsl_elementwise_saturate:
+  case Builtin::BI__builtin_hlsl_select: {

bharadwajy wrote:

This addition splits the existing common case code for 
`BI__builtin_hlsl_elemetwise_saturate` and `BI__builtin_hlsl_elemetwise_rcp` - 
and would result in incorrect behavior for 
`BI__builtin_hlsl_elemetwise_saturate`. 

https://github.com/llvm/llvm-project/pull/107129
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] Add normalize builtins and normalize HLSL function to DirectX and SPIR-V backend (PR #102683)

2024-08-13 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -229,6 +230,75 @@ static bool expandLog10Intrinsic(CallInst *Orig) {
   return expandLogIntrinsic(Orig, numbers::ln2f / numbers::ln10f);
 }
 
+static bool expandNormalizeIntrinsic(CallInst *Orig) {
+  Value *X = Orig->getOperand(0);
+  Type *Ty = Orig->getType();
+  Type *EltTy = Ty->getScalarType();
+  IRBuilder<> Builder(Orig->getParent());
+  Builder.SetInsertPoint(Orig);
+
+  auto *XVec = dyn_cast(Ty);
+  if (!XVec) {
+if (auto *constantFP = dyn_cast(X)) {
+  const APFloat &fpVal = constantFP->getValueAPF();
+  if (fpVal.isZero())
+report_fatal_error(Twine("Invalid input scalar: length is zero"),
+   /* gen_crash_diag=*/false);
+}
+Value *Result = Builder.CreateFDiv(X, X);
+
+Orig->replaceAllUsesWith(Result);
+Orig->eraseFromParent();
+return true;
+  }
+
+  Value *Elt = Builder.CreateExtractElement(X, (uint64_t)0);

bharadwajy wrote:

I see a build failure due to unused variable `Elt`.

https://github.com/llvm/llvm-project/pull/102683
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL] Implement support for HLSL intrinsic - saturate (PR #104619)

2024-08-16 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy created 
https://github.com/llvm/llvm-project/pull/104619

Implement support for HLSL intrinsic saturate.
Implement DXIL codegen for the intrinsic saturate by lowering it to DXIL Op 
dx.saturate.
Implement SPIRV codegen by transforming saturate(x) to clamp(x, 0.0f, 1.0f).

Add tests for DXIL and SPIRV CodeGen.

>From 7f338f8843ed815641541664ce2eff72278aacab Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli 
Date: Thu, 1 Aug 2024 02:46:05 +
Subject: [PATCH] Implement support to compile HLSL intrinsic "saturate" to
 DXIL

Add SPIRV Codegen support to transform saturate(x) to clamp(x, 0.0, 1.0)
Add tests for DXIL and SPIRV CodeGen.
---
 clang/include/clang/Basic/Builtins.td |   6 +
 clang/lib/CodeGen/CGBuiltin.cpp   |   9 +
 clang/lib/CodeGen/CGHLSLRuntime.h |   1 +
 clang/lib/Headers/hlsl/hlsl_intrinsics.h  |  41 ++-
 clang/lib/Sema/SemaHLSL.cpp   |   3 +-
 clang/test/CodeGenHLSL/builtins/saturate.hlsl |  54 
 .../SemaHLSL/BuiltIns/saturate-errors.hlsl|  31 ++
 llvm/include/llvm/IR/IntrinsicsDirectX.td |   1 +
 llvm/include/llvm/IR/IntrinsicsSPIRV.td   |   3 +-
 llvm/lib/Target/DirectX/DXIL.td   |  10 +
 .../Target/DirectX/DXILIntrinsicExpansion.cpp |  32 ++
 .../Target/SPIRV/SPIRVInstructionSelector.cpp |  80 -
 llvm/test/CodeGen/DirectX/saturate.ll | 276 ++
 llvm/test/CodeGen/DirectX/saturate_errors.ll  |  14 +
 .../CodeGen/SPIRV/hlsl-intrinsics/saturate.ll |  83 ++
 15 files changed, 627 insertions(+), 17 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/builtins/saturate.hlsl
 create mode 100644 clang/test/SemaHLSL/BuiltIns/saturate-errors.hlsl
 create mode 100644 llvm/test/CodeGen/DirectX/saturate.ll
 create mode 100644 llvm/test/CodeGen/DirectX/saturate_errors.ll
 create mode 100644 llvm/test/CodeGen/SPIRV/hlsl-intrinsics/saturate.ll

diff --git a/clang/include/clang/Basic/Builtins.td 
b/clang/include/clang/Basic/Builtins.td
index 0a874d8638df43..76e893e38b671c 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4743,6 +4743,12 @@ def HLSLRSqrt : LangBuiltin<"HLSL_LANG"> {
   let Prototype = "void(...)";
 }
 
+def HLSLSaturate : LangBuiltin<"HLSL_LANG"> {
+  let Spellings = ["__builtin_hlsl_elementwise_saturate"];
+  let Attributes = [NoThrow, Const];
+  let Prototype = "void(...)";
+}
+
 // Builtins for XRay.
 def XRayCustomEvent : Builtin {
   let Spellings = ["__xray_customevent"];
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 1c0baeaee03632..01841774562f06 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -18666,6 +18666,15 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
 /*ReturnType=*/Op0->getType(), 
CGM.getHLSLRuntime().getRsqrtIntrinsic(),
 ArrayRef{Op0}, nullptr, "hlsl.rsqrt");
   }
+  case Builtin::BI__builtin_hlsl_elementwise_saturate: {
+Value *Op0 = EmitScalarExpr(E->getArg(0));
+if (!E->getArg(0)->getType()->hasFloatingRepresentation())
+  llvm_unreachable("saturate operand must have a float representation");
+return Builder.CreateIntrinsic(
+/*ReturnType=*/Op0->getType(),
+CGM.getHLSLRuntime().getSaturateIntrinsic(), ArrayRef{Op0},
+nullptr, "hlsl.saturate");
+  }
   case Builtin::BI__builtin_hlsl_wave_get_lane_index: {
 return EmitRuntimeCall(CGM.CreateRuntimeFunction(
 llvm::FunctionType::get(IntTy, {}, false), 
"__hlsl_wave_get_lane_index",
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h 
b/clang/lib/CodeGen/CGHLSLRuntime.h
index cd604bea2e763d..b1455b5779acf9 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.h
+++ b/clang/lib/CodeGen/CGHLSLRuntime.h
@@ -79,6 +79,7 @@ class CGHLSLRuntime {
   GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp)
   GENERATE_HLSL_INTRINSIC_FUNCTION(Normalize, normalize)
   GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt)
+  GENERATE_HLSL_INTRINSIC_FUNCTION(Saturate, saturate)
   GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
 
   
//===--===//
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h 
b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index 678cdc77f8a71b..6d38b668fe770e 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -916,7 +916,7 @@ float4 lerp(float4, float4, float4);
 /// \brief Returns the length of the specified floating-point vector.
 /// \param x [in] The vector of floats, or a scalar float.
 ///
-/// Length is based on the following formula: sqrt(x[0]^2 + x[1]^2 + �).
+/// Length is based on the following formula: sqrt(x[0]^2 + x[1]^2 + ...).
 
 _HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
 _HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
@@ -1564,6 +1564,45 @@ float3 round(float3);
 _HLSL_BUILTIN_ALIAS(__builtin_elementwise_roundeven)
 float4 round(float4);
 
+//===---

[clang] [HLSL] Change default linkage of HLSL functions and `groupshared` variables (PR #93336)

2024-06-07 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -108,3 +108,16 @@ behavior between Clang and DXC. Some examples include:
   diagnostic notifying the user of the conversion rather than silently altering
   precision relative to the other overloads (as FXC does) or generating code
   that will fail validation (as DXC does).
+
+Correctness improvements (bug fixes)
+
+
+Entry point functions & ``static`` keyword
+--
+Marking a shader entry point function ``static`` will result in an error.
+
+This is idential to DXC behavior when an entry point is specified as compiler

bharadwajy wrote:

```suggestion
This is identical to DXC behavior when an entry point is specified as compiler
```

https://github.com/llvm/llvm-project/pull/93336
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Set DXIL Version using shader model version in compilation target profile (PR #89823)

2024-04-23 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -68,25 +68,25 @@ TEST(DxcModeTest, TargetProfileValidation) {
   IntrusiveRefCntPtr DiagOpts = new DiagnosticOptions();
   DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagConsumer);
 
-  validateTargetProfile("-Tvs_6_0", "dxil--shadermodel6.0-vertex",
+  validateTargetProfile("-Tvs_6_0", "dxilv1.0--shadermodel6.0-vertex",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Ths_6_1", "dxil--shadermodel6.1-hull",
+  validateTargetProfile("-Ths_6_1", "dxilv1.1--shadermodel6.1-hull",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tds_6_2", "dxil--shadermodel6.2-domain",
+  validateTargetProfile("-Tds_6_2", "dxilv1.2--shadermodel6.2-domain",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tds_6_2", "dxil--shadermodel6.2-domain",
+  validateTargetProfile("-Tds_6_2", "dxilv1.2--shadermodel6.2-domain",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tgs_6_3", "dxil--shadermodel6.3-geometry",
+  validateTargetProfile("-Tgs_6_3", "dxilv1.3--shadermodel6.3-geometry",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tps_6_4", "dxil--shadermodel6.4-pixel",
+  validateTargetProfile("-Tps_6_4", "dxilv1.4--shadermodel6.4-pixel",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tcs_6_5", "dxil--shadermodel6.5-compute",
+  validateTargetProfile("-Tcs_6_5", "dxilv1.5--shadermodel6.5-compute",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tms_6_6", "dxil--shadermodel6.6-mesh",
+  validateTargetProfile("-Tms_6_6", "dxilv1.6--shadermodel6.6-mesh",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tas_6_7", "dxil--shadermodel6.7-amplification",
+  validateTargetProfile("-Tas_6_7", "dxilv1.7--shadermodel6.7-amplification",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tlib_6_x", "dxil--shadermodel6.15-library",
+  validateTargetProfile("-Tlib_6_8", "dxilv1.8--shadermodel6.8-library",

bharadwajy wrote:

> There is, in fact, a lib_6_x target that maps to a more unconstrained offline 
> linking target (with minor version = 15) which is not expected to be 
> compatible between compiler versions. Do we care to model that in clang at 
> this time? This change removes this target, so I just wanted to know if that 
> was a deliberate decision.

What is the expected DXIL version of DXIL Ops generated using the target 
profile `lib_6_x`?

https://github.com/llvm/llvm-project/pull/89823
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Set DXIL Version using shader model version in compilation target profile (PR #89823)

2024-04-23 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -98,9 +100,47 @@ std::optional tryParseProfile(StringRef 
Profile) {
   else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
 return std::nullopt;
 
-  // dxil-unknown-shadermodel-hull
+  // Determine DXIL version number using the minor version number of Shader
+  // Model version specified in target profile. Prior to decoupling DXIL 
version
+  // numbering from that of Shader Model DXIL version 1.Y corresponds to SM 
6.Y.
+  // E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
   llvm::Triple T;
-  T.setArch(Triple::ArchType::dxil);
+  Triple::SubArchType SubArch = llvm::Triple::NoSubArch;
+  switch (Minor) {
+  case 0:
+SubArch = llvm::Triple::DXILSubArch_v1_0;
+break;
+  case 1:
+SubArch = llvm::Triple::DXILSubArch_v1_1;
+break;
+  case 2:
+SubArch = llvm::Triple::DXILSubArch_v1_2;
+break;
+  case 3:
+SubArch = llvm::Triple::DXILSubArch_v1_3;
+break;
+  case 4:
+SubArch = llvm::Triple::DXILSubArch_v1_4;
+break;
+  case 5:
+SubArch = llvm::Triple::DXILSubArch_v1_5;
+break;
+  case 6:
+SubArch = llvm::Triple::DXILSubArch_v1_6;
+break;
+  case 7:
+SubArch = llvm::Triple::DXILSubArch_v1_7;
+break;
+  case 8:
+SubArch = llvm::Triple::DXILSubArch_v1_8;
+break;
+  }
+  if (SubArch == llvm::Triple::NoSubArch) {
+report_fatal_error(

bharadwajy wrote:

> The frontend should not call `report_fatal_error`. If this error is possible 
> we should emit a driver diagnostic (see the `D.Diag` calls elsewhere in this 
> file).
> 
> If this can't happen, we should instead assert.
> 
> If this error can occur we need a test that verifies the error condition.

Pushed a change to address the feedback. Thanks!

https://github.com/llvm/llvm-project/pull/89823
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Set DXIL Version using shader model version in compilation target profile (PR #89823)

2024-04-24 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -68,25 +68,25 @@ TEST(DxcModeTest, TargetProfileValidation) {
   IntrusiveRefCntPtr DiagOpts = new DiagnosticOptions();
   DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagConsumer);
 
-  validateTargetProfile("-Tvs_6_0", "dxil--shadermodel6.0-vertex",
+  validateTargetProfile("-Tvs_6_0", "dxilv1.0--shadermodel6.0-vertex",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Ths_6_1", "dxil--shadermodel6.1-hull",
+  validateTargetProfile("-Ths_6_1", "dxilv1.1--shadermodel6.1-hull",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tds_6_2", "dxil--shadermodel6.2-domain",
+  validateTargetProfile("-Tds_6_2", "dxilv1.2--shadermodel6.2-domain",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tds_6_2", "dxil--shadermodel6.2-domain",
+  validateTargetProfile("-Tds_6_2", "dxilv1.2--shadermodel6.2-domain",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tgs_6_3", "dxil--shadermodel6.3-geometry",
+  validateTargetProfile("-Tgs_6_3", "dxilv1.3--shadermodel6.3-geometry",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tps_6_4", "dxil--shadermodel6.4-pixel",
+  validateTargetProfile("-Tps_6_4", "dxilv1.4--shadermodel6.4-pixel",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tcs_6_5", "dxil--shadermodel6.5-compute",
+  validateTargetProfile("-Tcs_6_5", "dxilv1.5--shadermodel6.5-compute",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tms_6_6", "dxil--shadermodel6.6-mesh",
+  validateTargetProfile("-Tms_6_6", "dxilv1.6--shadermodel6.6-mesh",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tas_6_7", "dxil--shadermodel6.7-amplification",
+  validateTargetProfile("-Tas_6_7", "dxilv1.7--shadermodel6.7-amplification",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tlib_6_x", "dxil--shadermodel6.15-library",
+  validateTargetProfile("-Tlib_6_8", "dxilv1.8--shadermodel6.8-library",

bharadwajy wrote:

> `lib_6_x` should basically map to "whatever the latest DXIL version is"

In that case should the call be changed to 
```
validateTargetProfile("-Tlib_6_x", "dxilv1.8--shadermodel6.8-library",
InMemoryFileSystem, Diags);
```

and the option `-T6_x` be considered equivalent to `-T6_8` (as of 
now)?
 

https://github.com/llvm/llvm-project/pull/89823
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Set DXIL Version using shader model version in compilation target profile (PR #89823)

2024-04-24 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy edited 
https://github.com/llvm/llvm-project/pull/89823
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Set DXIL Version using shader model version in compilation target profile (PR #89823)

2024-04-24 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy edited 
https://github.com/llvm/llvm-project/pull/89823
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Set DXIL Version using shader model version in compilation target profile (PR #89823)

2024-04-24 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -68,25 +68,25 @@ TEST(DxcModeTest, TargetProfileValidation) {
   IntrusiveRefCntPtr DiagOpts = new DiagnosticOptions();
   DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagConsumer);
 
-  validateTargetProfile("-Tvs_6_0", "dxil--shadermodel6.0-vertex",
+  validateTargetProfile("-Tvs_6_0", "dxilv1.0--shadermodel6.0-vertex",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Ths_6_1", "dxil--shadermodel6.1-hull",
+  validateTargetProfile("-Ths_6_1", "dxilv1.1--shadermodel6.1-hull",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tds_6_2", "dxil--shadermodel6.2-domain",
+  validateTargetProfile("-Tds_6_2", "dxilv1.2--shadermodel6.2-domain",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tds_6_2", "dxil--shadermodel6.2-domain",
+  validateTargetProfile("-Tds_6_2", "dxilv1.2--shadermodel6.2-domain",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tgs_6_3", "dxil--shadermodel6.3-geometry",
+  validateTargetProfile("-Tgs_6_3", "dxilv1.3--shadermodel6.3-geometry",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tps_6_4", "dxil--shadermodel6.4-pixel",
+  validateTargetProfile("-Tps_6_4", "dxilv1.4--shadermodel6.4-pixel",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tcs_6_5", "dxil--shadermodel6.5-compute",
+  validateTargetProfile("-Tcs_6_5", "dxilv1.5--shadermodel6.5-compute",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tms_6_6", "dxil--shadermodel6.6-mesh",
+  validateTargetProfile("-Tms_6_6", "dxilv1.6--shadermodel6.6-mesh",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tas_6_7", "dxil--shadermodel6.7-amplification",
+  validateTargetProfile("-Tas_6_7", "dxilv1.7--shadermodel6.7-amplification",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tlib_6_x", "dxil--shadermodel6.15-library",
+  validateTargetProfile("-Tlib_6_8", "dxilv1.8--shadermodel6.8-library",

bharadwajy wrote:

> `lib_6_x` should basically map to "whatever the latest DXIL version is"

Pushed a change to consider `lib_6_x` as a valid profile equivalent to 
`lib_6_8`.

https://github.com/llvm/llvm-project/pull/89823
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Set DXIL Version using shader model version in compilation target profile (PR #89823)

2024-04-24 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -68,25 +68,25 @@ TEST(DxcModeTest, TargetProfileValidation) {
   IntrusiveRefCntPtr DiagOpts = new DiagnosticOptions();
   DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagConsumer);
 
-  validateTargetProfile("-Tvs_6_0", "dxil--shadermodel6.0-vertex",
+  validateTargetProfile("-Tvs_6_0", "dxilv1.0--shadermodel6.0-vertex",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Ths_6_1", "dxil--shadermodel6.1-hull",
+  validateTargetProfile("-Ths_6_1", "dxilv1.1--shadermodel6.1-hull",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tds_6_2", "dxil--shadermodel6.2-domain",
+  validateTargetProfile("-Tds_6_2", "dxilv1.2--shadermodel6.2-domain",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tds_6_2", "dxil--shadermodel6.2-domain",
+  validateTargetProfile("-Tds_6_2", "dxilv1.2--shadermodel6.2-domain",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tgs_6_3", "dxil--shadermodel6.3-geometry",
+  validateTargetProfile("-Tgs_6_3", "dxilv1.3--shadermodel6.3-geometry",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tps_6_4", "dxil--shadermodel6.4-pixel",
+  validateTargetProfile("-Tps_6_4", "dxilv1.4--shadermodel6.4-pixel",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tcs_6_5", "dxil--shadermodel6.5-compute",
+  validateTargetProfile("-Tcs_6_5", "dxilv1.5--shadermodel6.5-compute",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tms_6_6", "dxil--shadermodel6.6-mesh",
+  validateTargetProfile("-Tms_6_6", "dxilv1.6--shadermodel6.6-mesh",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tas_6_7", "dxil--shadermodel6.7-amplification",
+  validateTargetProfile("-Tas_6_7", "dxilv1.7--shadermodel6.7-amplification",
 InMemoryFileSystem, Diags);
-  validateTargetProfile("-Tlib_6_x", "dxil--shadermodel6.15-library",
+  validateTargetProfile("-Tlib_6_x", "dxilv1.8--shadermodel6.15-library",

bharadwajy wrote:

> Seems like we should have a test case for `lib_6_8` too since they're not the 
> same.

Added a test case.

https://github.com/llvm/llvm-project/pull/89823
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Set DXIL Version using shader model version in compilation target profile (PR #89823)

2024-04-26 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -2,39 +2,45 @@
 
 // Supported targets
 //
-// RUN: %clang -target dxil--shadermodel6.2-pixel %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-VALID %s
-// RUN: %clang -target dxil-unknown-shadermodel6.2-pixel %s -S -o /dev/null 
2>&1 | FileCheck --check-prefix=CHECK-VALID %s
-// RUN: %clang -target dxil--shadermodel6.2-library %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-VALID %s
-// RUN: %clang -target dxil-unknown-shadermodel6.2-library %s -S -o /dev/null 
2>&1 | FileCheck --check-prefix=CHECK-VALID %s
+// RUN: %clang -target dxilv1.2--shadermodel6.2-pixel %s -S -o /dev/null 2>&1 
| FileCheck --check-prefix=CHECK-VALID %s
+// RUN: %clang -target dxilv1.2-unknown-shadermodel6.2-pixel %s -S -o 
/dev/null 2>&1 | FileCheck --check-prefix=CHECK-VALID %s
+// RUN: %clang -target dxilv1.2--shadermodel6.2-library %s -S -o /dev/null 
2>&1 | FileCheck --check-prefix=CHECK-VALID %s
+// RUN: %clang -target dxilv1.2-unknown-shadermodel6.2-library %s -S -o 
/dev/null 2>&1 | FileCheck --check-prefix=CHECK-VALID %s
 
 // Empty shader model
 //
-// RUN: not %clang -target dxil %s -S -o /dev/null 2>&1 | FileCheck 
--check-prefix=CHECK-NO-OS %s
+// RUN: not %clang -target dxilv1.2 %s -S -o /dev/null 2>&1 | FileCheck 
--check-prefix=CHECK-NO-OS %s
 
 // Invalid shader models
 //
-// RUN: not %clang -target dxil--linux %s -S -o /dev/null 2>&1 | FileCheck 
--check-prefix=CHECK-BAD-OS %s
-// RUN: not %clang -target dxil--win32 %s -S -o /dev/null 2>&1 | FileCheck 
--check-prefix=CHECK-BAD-OS %s
-// RUN: not %clang -target dxil--unknown %s -S -o /dev/null 2>&1 | FileCheck 
--check-prefix=CHECK-BAD-OS %s
-// RUN: not %clang -target dxil--invalidos %s -S -o /dev/null 2>&1 | FileCheck 
--check-prefix=CHECK-BAD-OS %s
+// RUN: not %clang -target dxilv1.2--linux %s -S -o /dev/null 2>&1 | FileCheck 
--check-prefix=CHECK-BAD-OS %s
+// RUN: not %clang -target dxilv1.2--win32 %s -S -o /dev/null 2>&1 | FileCheck 
--check-prefix=CHECK-BAD-OS %s
+// RUN: not %clang -target dxilv1.2--unknown %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-BAD-OS %s
+// RUN: not %clang -target dxilv1.2--invalidos %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-BAD-OS %s
 
 // Bad shader model versions. Currently we just check for any version at all.
 //
-// RUN: not %clang -target dxil--shadermodel %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-BAD-OS %s
-// RUN: not %clang -target dxil--shadermodel0.0 %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-BAD-OS %s
+// RUN: not %clang -target dxilv1.2--shadermodel %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-BAD-OS %s
+// RUN: not %clang -target dxilv1.2--shadermodel0.0 %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-BAD-OS %s
 
 // Empty shader stage
 //
-// RUN: not %clang -target dxil-shadermodel6.2 %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-NO-ENV %s
-// RUN: not %clang -target dxil--shadermodel6.2 %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-NO-ENV %s
-// RUN: not %clang -target dxil--shadermodel6.2 %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-NO-ENV %s
+// RUN: not %clang -target dxilv1.2-shadermodel6.2 %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-NO-ENV %s
+// RUN: not %clang -target dxilv1.2--shadermodel6.2 %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-NO-ENV %s
+// RUN: not %clang -target dxilv1.2--shadermodel6.2 %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-NO-ENV %s
 
 // Invalid shader stages
 //
-// RUN: not %clang -target dxil--shadermodel6.2-unknown %s -S -o /dev/null 
2>&1 | FileCheck --check-prefix=CHECK-BAD-ENV %s
-// RUN: not %clang --target=dxil--shadermodel6.2-invalidenvironment %s -S -o 
/dev/null 2>&1 | FileCheck --check-prefix=CHECK-BAD-ENV-DRV %s
-// RUN: not %clang -target dxil--shadermodel6.2-eabi %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-BAD-ENV %s
-// RUN: not %clang -target dxil--shadermodel6.2-msvc %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-BAD-ENV %s
+// RUN: not %clang -target dxilv1.2--shadermodel6.2-unknown %s -S -o /dev/null 
2>&1 | FileCheck --check-prefix=CHECK-BAD-ENV %s
+// RUN: not %clang --target=dxilv1.2--shadermodel6.2-invalidenvironment %s -S 
-o /dev/null 2>&1 | FileCheck --check-prefix=CHECK-BAD-ENV-DRV %s
+// RUN: not %clang -target dxilv1.2--shadermodel6.2-eabi %s -S -o /dev/null 
2>&1 | FileCheck --check-prefix=CHECK-BAD-ENV %s
+// RUN: not %clang -target dxilv1.2--shadermodel6.2-msvc %s -S -o /dev/null 
2>&1 | FileCheck --check-prefix=CHECK-BAD-ENV %s
+
+// Invaliddxil target with no version number
+// RUN: not %clang -target dxil--shadermodel6.2-msvc %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-BAD-TARGET %s

bharadwajy wrote:

> So is the `dxil` arch with no subarch just completely disallowed then? 

Yes.

> This is unusual compared to other targets, and also a bit inconvenient for 
> testing. If I'm trying something locall

[clang] [llvm] [DirectX] Set DXIL Version using shader model version in compilation target profile (PR #89823)

2024-04-26 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -98,9 +103,47 @@ std::optional tryParseProfile(StringRef 
Profile) {
   else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
 return std::nullopt;
 
-  // dxil-unknown-shadermodel-hull
+  // Determine DXIL version number using the minor version number of Shader
+  // Model version specified in target profile. Prior to decoupling DXIL 
version
+  // numbering from that of Shader Model DXIL version 1.Y corresponds to SM 
6.Y.
+  // E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
   llvm::Triple T;
-  T.setArch(Triple::ArchType::dxil);
+  Triple::SubArchType SubArch = llvm::Triple::NoSubArch;
+  switch (Minor) {
+  case 0:
+SubArch = llvm::Triple::DXILSubArch_v1_0;
+break;
+  case 1:
+SubArch = llvm::Triple::DXILSubArch_v1_1;
+break;
+  case 2:
+SubArch = llvm::Triple::DXILSubArch_v1_2;
+break;
+  case 3:
+SubArch = llvm::Triple::DXILSubArch_v1_3;
+break;
+  case 4:
+SubArch = llvm::Triple::DXILSubArch_v1_4;
+break;
+  case 5:
+SubArch = llvm::Triple::DXILSubArch_v1_5;
+break;
+  case 6:
+SubArch = llvm::Triple::DXILSubArch_v1_6;
+break;
+  case 7:
+SubArch = llvm::Triple::DXILSubArch_v1_7;
+break;
+  case 8:
+  case OfflineLibMinor:
+// Always consider minor version x as the latest supported minor version
+SubArch = llvm::Triple::DXILSubArch_v1_8;

bharadwajy wrote:

> It might make updates here less error prone if we added `DXILSubArch_v1_x = 
> DXILSubArch_v1_8` or `LastDXILSubArch = DXILSubArch_v1_8` to the enum in 
> Triple.h, depending on which of those makes more logical sense.

OK.

https://github.com/llvm/llvm-project/pull/89823
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Set DXIL Version using shader model version in compilation target profile (PR #89823)

2024-04-26 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -2,39 +2,45 @@
 
 // Supported targets
 //
-// RUN: %clang -target dxil--shadermodel6.2-pixel %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-VALID %s
-// RUN: %clang -target dxil-unknown-shadermodel6.2-pixel %s -S -o /dev/null 
2>&1 | FileCheck --check-prefix=CHECK-VALID %s
-// RUN: %clang -target dxil--shadermodel6.2-library %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-VALID %s
-// RUN: %clang -target dxil-unknown-shadermodel6.2-library %s -S -o /dev/null 
2>&1 | FileCheck --check-prefix=CHECK-VALID %s
+// RUN: %clang -target dxilv1.2--shadermodel6.2-pixel %s -S -o /dev/null 2>&1 
| FileCheck --check-prefix=CHECK-VALID %s
+// RUN: %clang -target dxilv1.2-unknown-shadermodel6.2-pixel %s -S -o 
/dev/null 2>&1 | FileCheck --check-prefix=CHECK-VALID %s
+// RUN: %clang -target dxilv1.2--shadermodel6.2-library %s -S -o /dev/null 
2>&1 | FileCheck --check-prefix=CHECK-VALID %s
+// RUN: %clang -target dxilv1.2-unknown-shadermodel6.2-library %s -S -o 
/dev/null 2>&1 | FileCheck --check-prefix=CHECK-VALID %s
 
 // Empty shader model
 //
-// RUN: not %clang -target dxil %s -S -o /dev/null 2>&1 | FileCheck 
--check-prefix=CHECK-NO-OS %s
+// RUN: not %clang -target dxilv1.2 %s -S -o /dev/null 2>&1 | FileCheck 
--check-prefix=CHECK-NO-OS %s
 
 // Invalid shader models
 //
-// RUN: not %clang -target dxil--linux %s -S -o /dev/null 2>&1 | FileCheck 
--check-prefix=CHECK-BAD-OS %s
-// RUN: not %clang -target dxil--win32 %s -S -o /dev/null 2>&1 | FileCheck 
--check-prefix=CHECK-BAD-OS %s
-// RUN: not %clang -target dxil--unknown %s -S -o /dev/null 2>&1 | FileCheck 
--check-prefix=CHECK-BAD-OS %s
-// RUN: not %clang -target dxil--invalidos %s -S -o /dev/null 2>&1 | FileCheck 
--check-prefix=CHECK-BAD-OS %s
+// RUN: not %clang -target dxilv1.2--linux %s -S -o /dev/null 2>&1 | FileCheck 
--check-prefix=CHECK-BAD-OS %s
+// RUN: not %clang -target dxilv1.2--win32 %s -S -o /dev/null 2>&1 | FileCheck 
--check-prefix=CHECK-BAD-OS %s
+// RUN: not %clang -target dxilv1.2--unknown %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-BAD-OS %s
+// RUN: not %clang -target dxilv1.2--invalidos %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-BAD-OS %s
 
 // Bad shader model versions. Currently we just check for any version at all.
 //
-// RUN: not %clang -target dxil--shadermodel %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-BAD-OS %s
-// RUN: not %clang -target dxil--shadermodel0.0 %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-BAD-OS %s
+// RUN: not %clang -target dxilv1.2--shadermodel %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-BAD-OS %s
+// RUN: not %clang -target dxilv1.2--shadermodel0.0 %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-BAD-OS %s
 
 // Empty shader stage
 //
-// RUN: not %clang -target dxil-shadermodel6.2 %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-NO-ENV %s
-// RUN: not %clang -target dxil--shadermodel6.2 %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-NO-ENV %s
-// RUN: not %clang -target dxil--shadermodel6.2 %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-NO-ENV %s
+// RUN: not %clang -target dxilv1.2-shadermodel6.2 %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-NO-ENV %s
+// RUN: not %clang -target dxilv1.2--shadermodel6.2 %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-NO-ENV %s
+// RUN: not %clang -target dxilv1.2--shadermodel6.2 %s -S -o /dev/null 2>&1 | 
FileCheck --check-prefix=CHECK-NO-ENV %s

bharadwajy wrote:

> What should happen if someone specifies `dxilv1.0--shadermodel6.7` and then 
> uses some SM6.7 feature that isn't representable in dxil 1.0? We should 
> probably validate that the dxil version and shadermodel are compatible.

I'd expect such validation to be done during analysis and/or codegen passes - 
potentially specified in `DXIL.td` where the properties of DXIL Operations are 
specified.
 
> A 1-1 mapping for validating the shader model vs the dxil version is probably 
> sufficient / matches DXC. It's also possible we could do better, but I don't 
> know that it's worth the effort of cataloguing compatibility for that.

Such cataloguing would be part of `DXIL.td`.

https://github.com/llvm/llvm-project/pull/89823
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DXIL] Add tan intrinsic part 2 (PR #90277)

2024-04-30 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy approved this pull request.


https://github.com/llvm/llvm-project/pull/90277
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Shore up floating point conversions (PR #90222)

2024-05-01 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -2616,6 +2617,13 @@ bool Sema::IsFloatingPointPromotion(QualType FromType, 
QualType ToType) {
ToBuiltin->getKind() == BuiltinType::Ibm128))
 return true;
 
+  // In HLSL, `half` promotes to `float` or `double`, regardless of whether
+  // or not native half types are enabled.

bharadwajy wrote:

It appears that `half` is represented as LLVM IR type `half` with 
`-enable-16bit-types` option specified and is promoted to `float` without it 
being specified per [this](https://godbolt.org/z/53qc3a9PY).

https://github.com/llvm/llvm-project/pull/90222
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-01 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy created 
https://github.com/llvm/llvm-project/pull/90809

An earlier commit provided a way to decouple DXIL version from Shader Model 
version
by representing the DXIL version as `SubArch` in the DXIL Target Triple and 
adding 
corresponding valid DXIL Arch types.

This change constructs DXIL target triple with DXIL version that is deduced
from Shader Model version specified in the following scenarios:

1. When compilation target profile is specified: 
   For e.g., DXIL target triple `dxilv1.8-unknown-shader6.8-library` is 
constructed 
   when `-T lib_6_8` is specified. 
2. When DXIL target triple without DXIL version is specified:
For e.g., DXIL target triple `dxilv1.8-pc-shadermodel6.8-library` is 
constructed
when `-mtriple=dxil-pc-shadermodel6.8-library` is specified.

Note that this is an alternate / expanded implementation to that proposed in PR 
#89823.

PR #89823 implements functionality (1) above. However, it requires any DXIL 
target  
triple to explicitly include DXIL version anywhere DXIL target triple is 
expected (e.g., 
`dxilv1.8-pc-shadermodel6.8-library`) while a triple without DXIL version 
(e.g., `dxil-pc-shadermodel6.8-library`) is considered invalid. This 
functionality is
implemented as a change to `tryParseProfile()` and is included in this PR.

This PR adds the functionality (2) to eliminate the above requirement to 
explicitly 
specify DXIL version in the target triple thereby considering a triple without 
DXIL 
version ( e.g., `dxil-pc-shadermodel6.8-library`) to be valid. A triple with 
DXIL version 
is inferred based on the shader mode version specified. If no shader model 
version is 
specified, DXIL version is defaulted to 1.0. This functionality is implemented 
in the 
"Special case logic ..." section of `Triple::normalize()`. 
 
Updated relevant HLSL tests that check for target triple. 

Update one MIR test to reflect use of normalized target triple.

Validated that Clang (`check-clang`) and LLVM (`check-llvm`) regression tests 
pass. 

>From 1b6bb5bf115c9f72adde27b6d77d957edbc49321 Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli 
Date: Wed, 1 May 2024 14:42:42 -0400
Subject: [PATCH] Set DXIL Version in DXIL target triple based on shader model
 version a) specified as compilation target profile or b) specified as target
 triple string

Update relevant HLSL tests that check for target triple.
Update one MIR test to reflect use of normalized target triple.
---
 clang/lib/Basic/Targets.cpp   |  2 +-
 clang/lib/Driver/ToolChains/HLSL.cpp  | 44 +-
 clang/lib/Frontend/CompilerInvocation.cpp |  7 +--
 clang/test/CodeGenHLSL/basic-target.c |  2 +-
 clang/test/Driver/dxc_dxv_path.hlsl   |  6 +--
 .../enable_16bit_types_validation.hlsl|  4 +-
 clang/unittests/Driver/DXCModeTest.cpp| 22 -
 llvm/include/llvm/TargetParser/Triple.h   |  1 +
 llvm/lib/CodeGen/CommandFlags.cpp |  2 +-
 llvm/lib/IR/Verifier.cpp  |  4 +-
 llvm/lib/TargetParser/Triple.cpp  | 45 +++
 .../mir/infer-triple-unknown-target.mir   |  2 +-
 llvm/tools/opt/optdriver.cpp  |  2 +-
 13 files changed, 116 insertions(+), 27 deletions(-)

diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index e3283510c6aac7..dc1792b3471e6c 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -760,7 +760,7 @@ using namespace clang::targets;
 TargetInfo *
 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
  const std::shared_ptr &Opts) {
-  llvm::Triple Triple(Opts->Triple);
+  llvm::Triple Triple(llvm::Triple::normalize(Opts->Triple));
 
   // Construct the target
   std::unique_ptr Target = AllocateTarget(Triple, *Opts);
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp 
b/clang/lib/Driver/ToolChains/HLSL.cpp
index 1169b5d8c92dd6..c4c92613f44723 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -98,9 +98,49 @@ std::optional tryParseProfile(StringRef 
Profile) {
   else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
 return std::nullopt;
 
-  // dxil-unknown-shadermodel-hull
+  // Determine DXIL version using the minor version number of Shader
+  // Model version specified in target profile. Prior to decoupling DXIL 
version
+  // numbering from that of Shader Model DXIL version 1.Y corresponds to SM 
6.Y.
+  // E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
   llvm::Triple T;
-  T.setArch(Triple::ArchType::dxil);
+  Triple::SubArchType SubArch = llvm::Triple::NoSubArch;
+  switch (Minor) {
+  case 0:
+SubArch = llvm::Triple::DXILSubArch_v1_0;
+break;
+  case 1:
+SubArch = llvm::Triple::DXILSubArch_v1_1;
+break;
+  case 2:
+SubArch = llvm::Triple::DXILSubArch_v1_2;
+break;
+  case 3:
+SubArch = llvm::Triple::DXILSubArch_v1_3;
+break;
+  case 4:
+SubArch = llvm::Triple::DX

[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-02 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -115,6 +115,30 @@ StringRef Triple::getArchName(ArchType Kind, SubArchType 
SubArch) {
 if (SubArch == AArch64SubArch_arm64e)
   return "arm64e";
 break;
+  case Triple::dxil:
+switch (SubArch) {
+case Triple::NoSubArch:
+case Triple::DXILSubArch_v1_0:
+  return "dxilv1.0";
+case Triple::DXILSubArch_v1_1:
+  return "dxilv1.1";
+case Triple::DXILSubArch_v1_2:
+  return "dxilv1.2";
+case Triple::DXILSubArch_v1_3:
+  return "dxilv1.3";
+case Triple::DXILSubArch_v1_4:
+  return "dxilv1.4";
+case Triple::DXILSubArch_v1_5:
+  return "dxilv1.5";
+case Triple::DXILSubArch_v1_6:
+  return "dxilv1.6";
+case Triple::DXILSubArch_v1_7:
+  return "dxilv1.7";
+case Triple::DXILSubArch_v1_8:
+  return "dxilv1.8";
+default:
+  return "";

bharadwajy wrote:

> Would we want to llvm_unreachable or otherwise fail here, or does the caller 
> handle this case well?

The `default` case would be true for a call such as `getArchName(Triple::dxil, 
Triple::)`,
where `NonDXILSubArch` is something other than `DXILSubArch_v1_[0..8]` (e.g., 
`MipsSubArch_r6`).

I considered 3 options to handle this case:

1. Report failure / crash with a message indicating incorrect DXIL `SubArch`:  
not chosen as such behavior is not consistent with other `SuArch`s as pointed 
out [here](https://github.com/llvm/llvm-project/pull/89823/files#r1581314588) 
in the feedback for PR #89823.
2. Fall through to return the value of `getArchTypeName(Kind)` where `Kind` is 
`Triple::dxil` to return the string "dxil": not chosen since "dxil" does 
**not** represent the `ArchTypeName` for the `Arch/SubArch` combination of 
`Triple::dxil/Triple::` (e.g., 
`Triple::dxil//Triple::MipsSubArch_r6`) and returning "dxil" would indicate 
that it is a valid `ArchTypeName`.
3. Return null string (""): chosen to indicate that there is no valid 
`ArchTypeName` for the `Arch/SubArch` combination of 
`Triple::dxil/Triple::`(e.g., 
`Triple::dxil/Triple::MipsSubArch_r6`). The consumer of the Triple checks for 
validity of Arch to issue an error for a non-viable empty architecture name as 
I think it is a cleaner option compared to either reporting a failure or 
asserting here.


https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-02 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -760,7 +760,7 @@ using namespace clang::targets;
 TargetInfo *
 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
  const std::shared_ptr &Opts) {
-  llvm::Triple Triple(Opts->Triple);
+  llvm::Triple Triple(llvm::Triple::normalize(Opts->Triple));

bharadwajy wrote:

> I see. It is trying to add subarch for dxil. Could we allow Triple::NoSubArch 
> for dxil? 

`setArch(dxil, Triple::NoSubArch)` should work.

> Backend could do the job to translate NoSubArch to the version based on 
> shader model.

One of the principles I tried to adhere to in the implementation of part (2) is 
to pass an appropriately constructed DXIL target triple string at creation-time 
of (DXIL) TargetMachine instance to maintain consistency between all the fields 
of the instance of DXIL `Triple` viz., Data, Arch, SubArch etc. 

Delegating or expecting the backend to do the same may lead to inconsistencies 
in the Triple instance. What benefit do you see in letting Backend perform the 
same steps that can be performed at creation time? Of course, if the backend 
needs to change the triple information for any reason, it is still free to do 
so - but I think it should expect to have the target triple information that 
reflects command-line or IR module specification of target triple.


https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-02 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -115,6 +115,30 @@ StringRef Triple::getArchName(ArchType Kind, SubArchType 
SubArch) {
 if (SubArch == AArch64SubArch_arm64e)
   return "arm64e";
 break;
+  case Triple::dxil:
+switch (SubArch) {
+case Triple::NoSubArch:

bharadwajy wrote:

> Why default is dxil1.0?

No specific reason other than choose the most basic version. 

Your comment 
[here](https://github.com/llvm/llvm-project/pull/89823#discussion_r1581541937) 
appeared to indicate a similar default choice for SPIRV (viz., spirv32 being 
equivalent to spirv32_v1.0).

I am open to suggestions for alternate default DXIL version choices.

https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-02 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -4675,7 +4676,7 @@ bool CompilerInvocation::CreateFromArgsImpl(
   // FIXME: We shouldn't have to pass the DashX option around here
   InputKind DashX = Res.getFrontendOpts().DashX;
   ParseTargetArgs(Res.getTargetOpts(), Args, Diags);
-  llvm::Triple T(Res.getTargetOpts().Triple);
+  llvm::Triple T(llvm::Triple::normalize(Res.getTargetOpts().Triple));

bharadwajy wrote:

> Looks like part of this change is to normalize `Res.getTargetOpts().Triple` 
> in many places where it is used.
> 
> Would it be possible to arrange it so that this is normalized in one place 
> earlier on, so all users of `getTargetOpts()` can assume that it is 
> normalized already?

I believe the triple accessible using the expression `getTargetOpts().Triple` 
denotes the target options specified for the compilation. It seems appropriate 
to leave it unmodified (un-normalized) for reference or other purposes during 
compilation.  

https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Set DXIL Version using shader model version in compilation target profile (PR #89823)

2024-05-02 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy edited 
https://github.com/llvm/llvm-project/pull/89823
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy updated 
https://github.com/llvm/llvm-project/pull/90809

>From 1b6bb5bf115c9f72adde27b6d77d957edbc49321 Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli 
Date: Wed, 1 May 2024 14:42:42 -0400
Subject: [PATCH 1/2] Set DXIL Version in DXIL target triple based on shader
 model version a) specified as compilation target profile or b) specified as
 target triple string

Update relevant HLSL tests that check for target triple.
Update one MIR test to reflect use of normalized target triple.
---
 clang/lib/Basic/Targets.cpp   |  2 +-
 clang/lib/Driver/ToolChains/HLSL.cpp  | 44 +-
 clang/lib/Frontend/CompilerInvocation.cpp |  7 +--
 clang/test/CodeGenHLSL/basic-target.c |  2 +-
 clang/test/Driver/dxc_dxv_path.hlsl   |  6 +--
 .../enable_16bit_types_validation.hlsl|  4 +-
 clang/unittests/Driver/DXCModeTest.cpp| 22 -
 llvm/include/llvm/TargetParser/Triple.h   |  1 +
 llvm/lib/CodeGen/CommandFlags.cpp |  2 +-
 llvm/lib/IR/Verifier.cpp  |  4 +-
 llvm/lib/TargetParser/Triple.cpp  | 45 +++
 .../mir/infer-triple-unknown-target.mir   |  2 +-
 llvm/tools/opt/optdriver.cpp  |  2 +-
 13 files changed, 116 insertions(+), 27 deletions(-)

diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index e3283510c6aac7..dc1792b3471e6c 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -760,7 +760,7 @@ using namespace clang::targets;
 TargetInfo *
 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
  const std::shared_ptr &Opts) {
-  llvm::Triple Triple(Opts->Triple);
+  llvm::Triple Triple(llvm::Triple::normalize(Opts->Triple));
 
   // Construct the target
   std::unique_ptr Target = AllocateTarget(Triple, *Opts);
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp 
b/clang/lib/Driver/ToolChains/HLSL.cpp
index 1169b5d8c92dd6..c4c92613f44723 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -98,9 +98,49 @@ std::optional tryParseProfile(StringRef 
Profile) {
   else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
 return std::nullopt;
 
-  // dxil-unknown-shadermodel-hull
+  // Determine DXIL version using the minor version number of Shader
+  // Model version specified in target profile. Prior to decoupling DXIL 
version
+  // numbering from that of Shader Model DXIL version 1.Y corresponds to SM 
6.Y.
+  // E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
   llvm::Triple T;
-  T.setArch(Triple::ArchType::dxil);
+  Triple::SubArchType SubArch = llvm::Triple::NoSubArch;
+  switch (Minor) {
+  case 0:
+SubArch = llvm::Triple::DXILSubArch_v1_0;
+break;
+  case 1:
+SubArch = llvm::Triple::DXILSubArch_v1_1;
+break;
+  case 2:
+SubArch = llvm::Triple::DXILSubArch_v1_2;
+break;
+  case 3:
+SubArch = llvm::Triple::DXILSubArch_v1_3;
+break;
+  case 4:
+SubArch = llvm::Triple::DXILSubArch_v1_4;
+break;
+  case 5:
+SubArch = llvm::Triple::DXILSubArch_v1_5;
+break;
+  case 6:
+SubArch = llvm::Triple::DXILSubArch_v1_6;
+break;
+  case 7:
+SubArch = llvm::Triple::DXILSubArch_v1_7;
+break;
+  case 8:
+SubArch = llvm::Triple::DXILSubArch_v1_8;
+break;
+  case OfflineLibMinor:
+// Always consider minor version x as the latest supported DXIL version
+SubArch = llvm::Triple::LatestDXILSubArch;
+break;
+  default:
+// No DXIL Version corresponding to specified Shader Model version found
+return std::nullopt;
+  }
+  T.setArch(Triple::ArchType::dxil, SubArch);
   T.setOSName(Triple::getOSTypeName(Triple::OSType::ShaderModel).str() +
   VersionTuple(Major, Minor).getAsString());
   T.setEnvironment(Kind);
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 8312abc3603953..a2600174e02296 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -587,7 +587,8 @@ static bool FixupInvocation(CompilerInvocation &Invocation,
   LangOpts.SpeculativeLoadHardening = CodeGenOpts.SpeculativeLoadHardening;
   LangOpts.CurrentModule = LangOpts.ModuleName;
 
-  llvm::Triple T(TargetOpts.Triple);
+  llvm::Triple T(llvm::Triple::normalize(TargetOpts.Triple));
+
   llvm::Triple::ArchType Arch = T.getArch();
 
   CodeGenOpts.CodeModel = TargetOpts.CodeModel;
@@ -4675,7 +4676,7 @@ bool CompilerInvocation::CreateFromArgsImpl(
   // FIXME: We shouldn't have to pass the DashX option around here
   InputKind DashX = Res.getFrontendOpts().DashX;
   ParseTargetArgs(Res.getTargetOpts(), Args, Diags);
-  llvm::Triple T(Res.getTargetOpts().Triple);
+  llvm::Triple T(llvm::Triple::normalize(Res.getTargetOpts().Triple));
   ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), Args, Diags,
 Res.getFileSystemOpts().WorkingDir);
   ParseAPINotesArgs

[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -1200,6 +1224,27 @@ std::string Triple::normalize(StringRef Str) {
 }
   }
 
+  // Normalize DXIL triple if it does not include DXIL version number.
+  // Determine DXIL version number using the minor version number of Shader
+  // Model version specified in target triple, if any. Prior to decoupling DXIL
+  // version numbering from that of Shader Model DXIL version 1.Y corresponds 
to
+  // SM 6.Y. E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
+  if (Components[0] == "dxil") {
+std::string DXILVerStr{"dxilv1."};
+if (Components.size() > 2) {
+  // OS component specified
+  if (Components[2].starts_with("shadermodel6.")) {
+Components[0] = DXILVerStr.append(
+Components[2].drop_front(strlen("shadermodel6.")));
+  } else if (Components[2].starts_with("shadermodel")) {
+// If shader model specified is other than 6.x, set DXIL Version to 1.0
+Components[0] = DXILVerStr.append("0");
+  }
+}

bharadwajy wrote:

The function `normalize` does not parse for `SubArch`. So, `SubArch` is not 
available  for checking.

An input DXIL triple string `Str` with or without version (e.g., 
`dxil-pc-shadermodel6.3` or `dxilv1.3-pc-shadermodel6.3` will set `Arch` to 
`Triple::dxil`. Implicit deduction and insertion of DXIL version should be done 
only is `Component[0] == "dxil"` and not otherwise. Hence the string 
equivalence check for "dxil" and not for `Arch == Triple::dxil`.

However, made code changes to leverage Arch and OS values already parsed as 
suggested. A couple of additional sanity checks are also added.


https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -1200,6 +1224,27 @@ std::string Triple::normalize(StringRef Str) {
 }
   }
 
+  // Normalize DXIL triple if it does not include DXIL version number.
+  // Determine DXIL version number using the minor version number of Shader
+  // Model version specified in target triple, if any. Prior to decoupling DXIL
+  // version numbering from that of Shader Model DXIL version 1.Y corresponds 
to
+  // SM 6.Y. E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
+  if (Components[0] == "dxil") {
+std::string DXILVerStr{"dxilv1."};
+if (Components.size() > 2) {
+  // OS component specified
+  if (Components[2].starts_with("shadermodel6.")) {
+Components[0] = DXILVerStr.append(
+Components[2].drop_front(strlen("shadermodel6.")));
+  } else if (Components[2].starts_with("shadermodel")) {
+// If shader model specified is other than 6.x, set DXIL Version to 1.0
+Components[0] = DXILVerStr.append("0");
+  }
+}
+// DXIL version is not set for a non-specified OS string or one that does
+// not starts with shadermodel.
+  }
+

bharadwajy wrote:

> We should add unit tests to test the normalization.

Additional unit tests added.

https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy edited 
https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy updated 
https://github.com/llvm/llvm-project/pull/90809

>From 1b6bb5bf115c9f72adde27b6d77d957edbc49321 Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli 
Date: Wed, 1 May 2024 14:42:42 -0400
Subject: [PATCH 1/3] Set DXIL Version in DXIL target triple based on shader
 model version a) specified as compilation target profile or b) specified as
 target triple string

Update relevant HLSL tests that check for target triple.
Update one MIR test to reflect use of normalized target triple.
---
 clang/lib/Basic/Targets.cpp   |  2 +-
 clang/lib/Driver/ToolChains/HLSL.cpp  | 44 +-
 clang/lib/Frontend/CompilerInvocation.cpp |  7 +--
 clang/test/CodeGenHLSL/basic-target.c |  2 +-
 clang/test/Driver/dxc_dxv_path.hlsl   |  6 +--
 .../enable_16bit_types_validation.hlsl|  4 +-
 clang/unittests/Driver/DXCModeTest.cpp| 22 -
 llvm/include/llvm/TargetParser/Triple.h   |  1 +
 llvm/lib/CodeGen/CommandFlags.cpp |  2 +-
 llvm/lib/IR/Verifier.cpp  |  4 +-
 llvm/lib/TargetParser/Triple.cpp  | 45 +++
 .../mir/infer-triple-unknown-target.mir   |  2 +-
 llvm/tools/opt/optdriver.cpp  |  2 +-
 13 files changed, 116 insertions(+), 27 deletions(-)

diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index e3283510c6aac7..dc1792b3471e6c 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -760,7 +760,7 @@ using namespace clang::targets;
 TargetInfo *
 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
  const std::shared_ptr &Opts) {
-  llvm::Triple Triple(Opts->Triple);
+  llvm::Triple Triple(llvm::Triple::normalize(Opts->Triple));
 
   // Construct the target
   std::unique_ptr Target = AllocateTarget(Triple, *Opts);
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp 
b/clang/lib/Driver/ToolChains/HLSL.cpp
index 1169b5d8c92dd6..c4c92613f44723 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -98,9 +98,49 @@ std::optional tryParseProfile(StringRef 
Profile) {
   else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
 return std::nullopt;
 
-  // dxil-unknown-shadermodel-hull
+  // Determine DXIL version using the minor version number of Shader
+  // Model version specified in target profile. Prior to decoupling DXIL 
version
+  // numbering from that of Shader Model DXIL version 1.Y corresponds to SM 
6.Y.
+  // E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
   llvm::Triple T;
-  T.setArch(Triple::ArchType::dxil);
+  Triple::SubArchType SubArch = llvm::Triple::NoSubArch;
+  switch (Minor) {
+  case 0:
+SubArch = llvm::Triple::DXILSubArch_v1_0;
+break;
+  case 1:
+SubArch = llvm::Triple::DXILSubArch_v1_1;
+break;
+  case 2:
+SubArch = llvm::Triple::DXILSubArch_v1_2;
+break;
+  case 3:
+SubArch = llvm::Triple::DXILSubArch_v1_3;
+break;
+  case 4:
+SubArch = llvm::Triple::DXILSubArch_v1_4;
+break;
+  case 5:
+SubArch = llvm::Triple::DXILSubArch_v1_5;
+break;
+  case 6:
+SubArch = llvm::Triple::DXILSubArch_v1_6;
+break;
+  case 7:
+SubArch = llvm::Triple::DXILSubArch_v1_7;
+break;
+  case 8:
+SubArch = llvm::Triple::DXILSubArch_v1_8;
+break;
+  case OfflineLibMinor:
+// Always consider minor version x as the latest supported DXIL version
+SubArch = llvm::Triple::LatestDXILSubArch;
+break;
+  default:
+// No DXIL Version corresponding to specified Shader Model version found
+return std::nullopt;
+  }
+  T.setArch(Triple::ArchType::dxil, SubArch);
   T.setOSName(Triple::getOSTypeName(Triple::OSType::ShaderModel).str() +
   VersionTuple(Major, Minor).getAsString());
   T.setEnvironment(Kind);
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 8312abc3603953..a2600174e02296 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -587,7 +587,8 @@ static bool FixupInvocation(CompilerInvocation &Invocation,
   LangOpts.SpeculativeLoadHardening = CodeGenOpts.SpeculativeLoadHardening;
   LangOpts.CurrentModule = LangOpts.ModuleName;
 
-  llvm::Triple T(TargetOpts.Triple);
+  llvm::Triple T(llvm::Triple::normalize(TargetOpts.Triple));
+
   llvm::Triple::ArchType Arch = T.getArch();
 
   CodeGenOpts.CodeModel = TargetOpts.CodeModel;
@@ -4675,7 +4676,7 @@ bool CompilerInvocation::CreateFromArgsImpl(
   // FIXME: We shouldn't have to pass the DashX option around here
   InputKind DashX = Res.getFrontendOpts().DashX;
   ParseTargetArgs(Res.getTargetOpts(), Args, Diags);
-  llvm::Triple T(Res.getTargetOpts().Triple);
+  llvm::Triple T(llvm::Triple::normalize(Res.getTargetOpts().Triple));
   ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), Args, Diags,
 Res.getFileSystemOpts().WorkingDir);
   ParseAPINotesArgs

[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -4675,7 +4676,7 @@ bool CompilerInvocation::CreateFromArgsImpl(
   // FIXME: We shouldn't have to pass the DashX option around here
   InputKind DashX = Res.getFrontendOpts().DashX;
   ParseTargetArgs(Res.getTargetOpts(), Args, Diags);
-  llvm::Triple T(Res.getTargetOpts().Triple);
+  llvm::Triple T(llvm::Triple::normalize(Res.getTargetOpts().Triple));

bharadwajy wrote:

> IIUC `getTargetOpts().Triple` should _already_ be normalized. The definition 
> of the option includes a "Normalizer", like so:

Thanks for pointing this out. deleted the extraneously added calls to 
`normalize()`.

https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy updated 
https://github.com/llvm/llvm-project/pull/90809

>From 1b6bb5bf115c9f72adde27b6d77d957edbc49321 Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli 
Date: Wed, 1 May 2024 14:42:42 -0400
Subject: [PATCH 1/4] Set DXIL Version in DXIL target triple based on shader
 model version a) specified as compilation target profile or b) specified as
 target triple string

Update relevant HLSL tests that check for target triple.
Update one MIR test to reflect use of normalized target triple.
---
 clang/lib/Basic/Targets.cpp   |  2 +-
 clang/lib/Driver/ToolChains/HLSL.cpp  | 44 +-
 clang/lib/Frontend/CompilerInvocation.cpp |  7 +--
 clang/test/CodeGenHLSL/basic-target.c |  2 +-
 clang/test/Driver/dxc_dxv_path.hlsl   |  6 +--
 .../enable_16bit_types_validation.hlsl|  4 +-
 clang/unittests/Driver/DXCModeTest.cpp| 22 -
 llvm/include/llvm/TargetParser/Triple.h   |  1 +
 llvm/lib/CodeGen/CommandFlags.cpp |  2 +-
 llvm/lib/IR/Verifier.cpp  |  4 +-
 llvm/lib/TargetParser/Triple.cpp  | 45 +++
 .../mir/infer-triple-unknown-target.mir   |  2 +-
 llvm/tools/opt/optdriver.cpp  |  2 +-
 13 files changed, 116 insertions(+), 27 deletions(-)

diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index e3283510c6aac7..dc1792b3471e6c 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -760,7 +760,7 @@ using namespace clang::targets;
 TargetInfo *
 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
  const std::shared_ptr &Opts) {
-  llvm::Triple Triple(Opts->Triple);
+  llvm::Triple Triple(llvm::Triple::normalize(Opts->Triple));
 
   // Construct the target
   std::unique_ptr Target = AllocateTarget(Triple, *Opts);
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp 
b/clang/lib/Driver/ToolChains/HLSL.cpp
index 1169b5d8c92dd6..c4c92613f44723 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -98,9 +98,49 @@ std::optional tryParseProfile(StringRef 
Profile) {
   else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
 return std::nullopt;
 
-  // dxil-unknown-shadermodel-hull
+  // Determine DXIL version using the minor version number of Shader
+  // Model version specified in target profile. Prior to decoupling DXIL 
version
+  // numbering from that of Shader Model DXIL version 1.Y corresponds to SM 
6.Y.
+  // E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
   llvm::Triple T;
-  T.setArch(Triple::ArchType::dxil);
+  Triple::SubArchType SubArch = llvm::Triple::NoSubArch;
+  switch (Minor) {
+  case 0:
+SubArch = llvm::Triple::DXILSubArch_v1_0;
+break;
+  case 1:
+SubArch = llvm::Triple::DXILSubArch_v1_1;
+break;
+  case 2:
+SubArch = llvm::Triple::DXILSubArch_v1_2;
+break;
+  case 3:
+SubArch = llvm::Triple::DXILSubArch_v1_3;
+break;
+  case 4:
+SubArch = llvm::Triple::DXILSubArch_v1_4;
+break;
+  case 5:
+SubArch = llvm::Triple::DXILSubArch_v1_5;
+break;
+  case 6:
+SubArch = llvm::Triple::DXILSubArch_v1_6;
+break;
+  case 7:
+SubArch = llvm::Triple::DXILSubArch_v1_7;
+break;
+  case 8:
+SubArch = llvm::Triple::DXILSubArch_v1_8;
+break;
+  case OfflineLibMinor:
+// Always consider minor version x as the latest supported DXIL version
+SubArch = llvm::Triple::LatestDXILSubArch;
+break;
+  default:
+// No DXIL Version corresponding to specified Shader Model version found
+return std::nullopt;
+  }
+  T.setArch(Triple::ArchType::dxil, SubArch);
   T.setOSName(Triple::getOSTypeName(Triple::OSType::ShaderModel).str() +
   VersionTuple(Major, Minor).getAsString());
   T.setEnvironment(Kind);
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 8312abc3603953..a2600174e02296 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -587,7 +587,8 @@ static bool FixupInvocation(CompilerInvocation &Invocation,
   LangOpts.SpeculativeLoadHardening = CodeGenOpts.SpeculativeLoadHardening;
   LangOpts.CurrentModule = LangOpts.ModuleName;
 
-  llvm::Triple T(TargetOpts.Triple);
+  llvm::Triple T(llvm::Triple::normalize(TargetOpts.Triple));
+
   llvm::Triple::ArchType Arch = T.getArch();
 
   CodeGenOpts.CodeModel = TargetOpts.CodeModel;
@@ -4675,7 +4676,7 @@ bool CompilerInvocation::CreateFromArgsImpl(
   // FIXME: We shouldn't have to pass the DashX option around here
   InputKind DashX = Res.getFrontendOpts().DashX;
   ParseTargetArgs(Res.getTargetOpts(), Args, Diags);
-  llvm::Triple T(Res.getTargetOpts().Triple);
+  llvm::Triple T(llvm::Triple::normalize(Res.getTargetOpts().Triple));
   ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), Args, Diags,
 Res.getFileSystemOpts().WorkingDir);
   ParseAPINotesArgs

[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -744,7 +744,7 @@ void codegen::setFunctionAttributes(StringRef CPU, 
StringRef Features,
 Expected>
 codegen::createTargetMachineForTriple(StringRef TargetTriple,
   CodeGenOptLevel OptLevel) {
-  Triple TheTriple(TargetTriple);
+  Triple TheTriple(llvm::Triple::normalize(TargetTriple.str()));

bharadwajy wrote:

> It's a bit of a grey area because this function is only really used by 
> testing tools but I don't think we should normalize here. If someone is 
> running llvm-reduce or one of the fuzzers on a module with an invalid triple 
> they may well be doing that intentionally.

OK. Deleted the call to `normalize()` and restored the MIR test.

https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy updated 
https://github.com/llvm/llvm-project/pull/90809

>From 1b6bb5bf115c9f72adde27b6d77d957edbc49321 Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli 
Date: Wed, 1 May 2024 14:42:42 -0400
Subject: [PATCH 1/5] Set DXIL Version in DXIL target triple based on shader
 model version a) specified as compilation target profile or b) specified as
 target triple string

Update relevant HLSL tests that check for target triple.
Update one MIR test to reflect use of normalized target triple.
---
 clang/lib/Basic/Targets.cpp   |  2 +-
 clang/lib/Driver/ToolChains/HLSL.cpp  | 44 +-
 clang/lib/Frontend/CompilerInvocation.cpp |  7 +--
 clang/test/CodeGenHLSL/basic-target.c |  2 +-
 clang/test/Driver/dxc_dxv_path.hlsl   |  6 +--
 .../enable_16bit_types_validation.hlsl|  4 +-
 clang/unittests/Driver/DXCModeTest.cpp| 22 -
 llvm/include/llvm/TargetParser/Triple.h   |  1 +
 llvm/lib/CodeGen/CommandFlags.cpp |  2 +-
 llvm/lib/IR/Verifier.cpp  |  4 +-
 llvm/lib/TargetParser/Triple.cpp  | 45 +++
 .../mir/infer-triple-unknown-target.mir   |  2 +-
 llvm/tools/opt/optdriver.cpp  |  2 +-
 13 files changed, 116 insertions(+), 27 deletions(-)

diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index e3283510c6aac7..dc1792b3471e6c 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -760,7 +760,7 @@ using namespace clang::targets;
 TargetInfo *
 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
  const std::shared_ptr &Opts) {
-  llvm::Triple Triple(Opts->Triple);
+  llvm::Triple Triple(llvm::Triple::normalize(Opts->Triple));
 
   // Construct the target
   std::unique_ptr Target = AllocateTarget(Triple, *Opts);
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp 
b/clang/lib/Driver/ToolChains/HLSL.cpp
index 1169b5d8c92dd6..c4c92613f44723 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -98,9 +98,49 @@ std::optional tryParseProfile(StringRef 
Profile) {
   else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
 return std::nullopt;
 
-  // dxil-unknown-shadermodel-hull
+  // Determine DXIL version using the minor version number of Shader
+  // Model version specified in target profile. Prior to decoupling DXIL 
version
+  // numbering from that of Shader Model DXIL version 1.Y corresponds to SM 
6.Y.
+  // E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
   llvm::Triple T;
-  T.setArch(Triple::ArchType::dxil);
+  Triple::SubArchType SubArch = llvm::Triple::NoSubArch;
+  switch (Minor) {
+  case 0:
+SubArch = llvm::Triple::DXILSubArch_v1_0;
+break;
+  case 1:
+SubArch = llvm::Triple::DXILSubArch_v1_1;
+break;
+  case 2:
+SubArch = llvm::Triple::DXILSubArch_v1_2;
+break;
+  case 3:
+SubArch = llvm::Triple::DXILSubArch_v1_3;
+break;
+  case 4:
+SubArch = llvm::Triple::DXILSubArch_v1_4;
+break;
+  case 5:
+SubArch = llvm::Triple::DXILSubArch_v1_5;
+break;
+  case 6:
+SubArch = llvm::Triple::DXILSubArch_v1_6;
+break;
+  case 7:
+SubArch = llvm::Triple::DXILSubArch_v1_7;
+break;
+  case 8:
+SubArch = llvm::Triple::DXILSubArch_v1_8;
+break;
+  case OfflineLibMinor:
+// Always consider minor version x as the latest supported DXIL version
+SubArch = llvm::Triple::LatestDXILSubArch;
+break;
+  default:
+// No DXIL Version corresponding to specified Shader Model version found
+return std::nullopt;
+  }
+  T.setArch(Triple::ArchType::dxil, SubArch);
   T.setOSName(Triple::getOSTypeName(Triple::OSType::ShaderModel).str() +
   VersionTuple(Major, Minor).getAsString());
   T.setEnvironment(Kind);
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 8312abc3603953..a2600174e02296 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -587,7 +587,8 @@ static bool FixupInvocation(CompilerInvocation &Invocation,
   LangOpts.SpeculativeLoadHardening = CodeGenOpts.SpeculativeLoadHardening;
   LangOpts.CurrentModule = LangOpts.ModuleName;
 
-  llvm::Triple T(TargetOpts.Triple);
+  llvm::Triple T(llvm::Triple::normalize(TargetOpts.Triple));
+
   llvm::Triple::ArchType Arch = T.getArch();
 
   CodeGenOpts.CodeModel = TargetOpts.CodeModel;
@@ -4675,7 +4676,7 @@ bool CompilerInvocation::CreateFromArgsImpl(
   // FIXME: We shouldn't have to pass the DashX option around here
   InputKind DashX = Res.getFrontendOpts().DashX;
   ParseTargetArgs(Res.getTargetOpts(), Args, Diags);
-  llvm::Triple T(Res.getTargetOpts().Triple);
+  llvm::Triple T(llvm::Triple::normalize(Res.getTargetOpts().Triple));
   ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), Args, Diags,
 Res.getFileSystemOpts().WorkingDir);
   ParseAPINotesArgs

[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -630,7 +630,7 @@ extern "C" int optMain(
 }
   }
 
-  Triple ModuleTriple(M->getTargetTriple());
+  Triple ModuleTriple(Triple::normalize(M->getTargetTriple()));

bharadwajy wrote:

> Similarly to my concern about updating `createTargetMachineForTriple`, I 
> don't think opt should messing with the triple it's given.

Deleted call to `normalize()`.

https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy updated 
https://github.com/llvm/llvm-project/pull/90809

>From 1b6bb5bf115c9f72adde27b6d77d957edbc49321 Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli 
Date: Wed, 1 May 2024 14:42:42 -0400
Subject: [PATCH 1/6] Set DXIL Version in DXIL target triple based on shader
 model version a) specified as compilation target profile or b) specified as
 target triple string

Update relevant HLSL tests that check for target triple.
Update one MIR test to reflect use of normalized target triple.
---
 clang/lib/Basic/Targets.cpp   |  2 +-
 clang/lib/Driver/ToolChains/HLSL.cpp  | 44 +-
 clang/lib/Frontend/CompilerInvocation.cpp |  7 +--
 clang/test/CodeGenHLSL/basic-target.c |  2 +-
 clang/test/Driver/dxc_dxv_path.hlsl   |  6 +--
 .../enable_16bit_types_validation.hlsl|  4 +-
 clang/unittests/Driver/DXCModeTest.cpp| 22 -
 llvm/include/llvm/TargetParser/Triple.h   |  1 +
 llvm/lib/CodeGen/CommandFlags.cpp |  2 +-
 llvm/lib/IR/Verifier.cpp  |  4 +-
 llvm/lib/TargetParser/Triple.cpp  | 45 +++
 .../mir/infer-triple-unknown-target.mir   |  2 +-
 llvm/tools/opt/optdriver.cpp  |  2 +-
 13 files changed, 116 insertions(+), 27 deletions(-)

diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index e3283510c6aac7..dc1792b3471e6c 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -760,7 +760,7 @@ using namespace clang::targets;
 TargetInfo *
 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
  const std::shared_ptr &Opts) {
-  llvm::Triple Triple(Opts->Triple);
+  llvm::Triple Triple(llvm::Triple::normalize(Opts->Triple));
 
   // Construct the target
   std::unique_ptr Target = AllocateTarget(Triple, *Opts);
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp 
b/clang/lib/Driver/ToolChains/HLSL.cpp
index 1169b5d8c92dd6..c4c92613f44723 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -98,9 +98,49 @@ std::optional tryParseProfile(StringRef 
Profile) {
   else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
 return std::nullopt;
 
-  // dxil-unknown-shadermodel-hull
+  // Determine DXIL version using the minor version number of Shader
+  // Model version specified in target profile. Prior to decoupling DXIL 
version
+  // numbering from that of Shader Model DXIL version 1.Y corresponds to SM 
6.Y.
+  // E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
   llvm::Triple T;
-  T.setArch(Triple::ArchType::dxil);
+  Triple::SubArchType SubArch = llvm::Triple::NoSubArch;
+  switch (Minor) {
+  case 0:
+SubArch = llvm::Triple::DXILSubArch_v1_0;
+break;
+  case 1:
+SubArch = llvm::Triple::DXILSubArch_v1_1;
+break;
+  case 2:
+SubArch = llvm::Triple::DXILSubArch_v1_2;
+break;
+  case 3:
+SubArch = llvm::Triple::DXILSubArch_v1_3;
+break;
+  case 4:
+SubArch = llvm::Triple::DXILSubArch_v1_4;
+break;
+  case 5:
+SubArch = llvm::Triple::DXILSubArch_v1_5;
+break;
+  case 6:
+SubArch = llvm::Triple::DXILSubArch_v1_6;
+break;
+  case 7:
+SubArch = llvm::Triple::DXILSubArch_v1_7;
+break;
+  case 8:
+SubArch = llvm::Triple::DXILSubArch_v1_8;
+break;
+  case OfflineLibMinor:
+// Always consider minor version x as the latest supported DXIL version
+SubArch = llvm::Triple::LatestDXILSubArch;
+break;
+  default:
+// No DXIL Version corresponding to specified Shader Model version found
+return std::nullopt;
+  }
+  T.setArch(Triple::ArchType::dxil, SubArch);
   T.setOSName(Triple::getOSTypeName(Triple::OSType::ShaderModel).str() +
   VersionTuple(Major, Minor).getAsString());
   T.setEnvironment(Kind);
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 8312abc3603953..a2600174e02296 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -587,7 +587,8 @@ static bool FixupInvocation(CompilerInvocation &Invocation,
   LangOpts.SpeculativeLoadHardening = CodeGenOpts.SpeculativeLoadHardening;
   LangOpts.CurrentModule = LangOpts.ModuleName;
 
-  llvm::Triple T(TargetOpts.Triple);
+  llvm::Triple T(llvm::Triple::normalize(TargetOpts.Triple));
+
   llvm::Triple::ArchType Arch = T.getArch();
 
   CodeGenOpts.CodeModel = TargetOpts.CodeModel;
@@ -4675,7 +4676,7 @@ bool CompilerInvocation::CreateFromArgsImpl(
   // FIXME: We shouldn't have to pass the DashX option around here
   InputKind DashX = Res.getFrontendOpts().DashX;
   ParseTargetArgs(Res.getTargetOpts(), Args, Diags);
-  llvm::Triple T(Res.getTargetOpts().Triple);
+  llvm::Triple T(llvm::Triple::normalize(Res.getTargetOpts().Triple));
   ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), Args, Diags,
 Res.getFileSystemOpts().WorkingDir);
   ParseAPINotesArgs

[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -744,7 +744,7 @@ void codegen::setFunctionAttributes(StringRef CPU, 
StringRef Features,
 Expected>
 codegen::createTargetMachineForTriple(StringRef TargetTriple,
   CodeGenOptLevel OptLevel) {
-  Triple TheTriple(TargetTriple);
+  Triple TheTriple(TargetTriple.str());

bharadwajy wrote:

> This looks redundant - doesn't Triple's constructor do the conversion to 
> `std::string` from Twine internally?

Thanks for flagging my incomplete restoration. Fixed.

https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -115,6 +115,30 @@ StringRef Triple::getArchName(ArchType Kind, SubArchType 
SubArch) {
 if (SubArch == AArch64SubArch_arm64e)
   return "arm64e";
 break;
+  case Triple::dxil:
+switch (SubArch) {
+case Triple::NoSubArch:
+case Triple::DXILSubArch_v1_0:
+  return "dxilv1.0";
+case Triple::DXILSubArch_v1_1:
+  return "dxilv1.1";
+case Triple::DXILSubArch_v1_2:
+  return "dxilv1.2";
+case Triple::DXILSubArch_v1_3:
+  return "dxilv1.3";
+case Triple::DXILSubArch_v1_4:
+  return "dxilv1.4";
+case Triple::DXILSubArch_v1_5:
+  return "dxilv1.5";
+case Triple::DXILSubArch_v1_6:
+  return "dxilv1.6";
+case Triple::DXILSubArch_v1_7:
+  return "dxilv1.7";
+case Triple::DXILSubArch_v1_8:
+  return "dxilv1.8";
+default:
+  return "";

bharadwajy wrote:

> I would argue that returning an empty string here is just as inconsistent as 
> erroring out/crashing. We should probably just do (2) even though it's 
> obviously ridiculous, since for other architectures if you provide an invalid 
> subarch they are indeed just as ridiculous.

OK. Pushed a change that uses option (2).

https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -2454,4 +2454,19 @@ TEST(TripleTest, isArmMClass) {
 EXPECT_TRUE(T.isArmMClass());
   }
 }
+
+TEST(TripleTest, DXILNormaizeWithVersion) {
+  EXPECT_EQ("dxilv1.0-unknown-shadermodel6.0",
+Triple::normalize("dxilv1.0--shadermodel6.0"));

bharadwajy wrote:

> What will Triple::normalize("dxilv1.0--shadermodel6.2") expect equal? 
> "dxilv1.2-unknown-shadermodel6.2"?

No. Since DXIL version is specified in the triple, it is honored as is. So, it 
will be `dxilv1.0-unknown-shadermodel6.2`

https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy edited 
https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -2454,4 +2454,19 @@ TEST(TripleTest, isArmMClass) {
 EXPECT_TRUE(T.isArmMClass());
   }
 }
+
+TEST(TripleTest, DXILNormaizeWithVersion) {
+  EXPECT_EQ("dxilv1.0-unknown-shadermodel6.0",
+Triple::normalize("dxilv1.0--shadermodel6.0"));

bharadwajy wrote:

> So Triple::normalize("dxilv1.2--shadermodel6.0") will get 
> "dxilv1.2-unknown-shadermodel6.0"?

Yes.

https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -2454,4 +2454,19 @@ TEST(TripleTest, isArmMClass) {
 EXPECT_TRUE(T.isArmMClass());
   }
 }
+
+TEST(TripleTest, DXILNormaizeWithVersion) {
+  EXPECT_EQ("dxilv1.0-unknown-shadermodel6.0",
+Triple::normalize("dxilv1.0--shadermodel6.0"));

bharadwajy wrote:

> Do we need a rule for what is legal or not?

I expect such validation to be part of later compiler passes.

https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy updated 
https://github.com/llvm/llvm-project/pull/90809

>From 3b74e41492aeb916487105b05316f8db255c57c3 Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli 
Date: Wed, 1 May 2024 14:42:42 -0400
Subject: [PATCH 1/6] Set DXIL Version in DXIL target triple based on shader
 model version a) specified as compilation target profile or b) specified as
 target triple string

Update relevant HLSL tests that check for target triple.
---
 clang/lib/Basic/Targets.cpp   |  2 +-
 clang/lib/Driver/ToolChains/HLSL.cpp  | 44 +-
 clang/lib/Frontend/CompilerInvocation.cpp |  7 +--
 clang/test/CodeGenHLSL/basic-target.c |  2 +-
 clang/test/Driver/dxc_dxv_path.hlsl   |  6 +--
 .../enable_16bit_types_validation.hlsl|  4 +-
 clang/unittests/Driver/DXCModeTest.cpp| 22 -
 llvm/include/llvm/TargetParser/Triple.h   |  1 +
 llvm/lib/CodeGen/CommandFlags.cpp |  2 +-
 llvm/lib/IR/Verifier.cpp  |  4 +-
 llvm/lib/TargetParser/Triple.cpp  | 45 +++
 .../mir/infer-triple-unknown-target.mir   |  2 +-
 llvm/tools/opt/optdriver.cpp  |  2 +-
 13 files changed, 116 insertions(+), 27 deletions(-)

diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index e3283510c6aac7..dc1792b3471e6c 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -760,7 +760,7 @@ using namespace clang::targets;
 TargetInfo *
 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
  const std::shared_ptr &Opts) {
-  llvm::Triple Triple(Opts->Triple);
+  llvm::Triple Triple(llvm::Triple::normalize(Opts->Triple));
 
   // Construct the target
   std::unique_ptr Target = AllocateTarget(Triple, *Opts);
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp 
b/clang/lib/Driver/ToolChains/HLSL.cpp
index 558e4db46f8182..8286e3be21803f 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -98,9 +98,49 @@ std::optional tryParseProfile(StringRef 
Profile) {
   else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
 return std::nullopt;
 
-  // dxil-unknown-shadermodel-hull
+  // Determine DXIL version using the minor version number of Shader
+  // Model version specified in target profile. Prior to decoupling DXIL 
version
+  // numbering from that of Shader Model DXIL version 1.Y corresponds to SM 
6.Y.
+  // E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
   llvm::Triple T;
-  T.setArch(Triple::ArchType::dxil);
+  Triple::SubArchType SubArch = llvm::Triple::NoSubArch;
+  switch (Minor) {
+  case 0:
+SubArch = llvm::Triple::DXILSubArch_v1_0;
+break;
+  case 1:
+SubArch = llvm::Triple::DXILSubArch_v1_1;
+break;
+  case 2:
+SubArch = llvm::Triple::DXILSubArch_v1_2;
+break;
+  case 3:
+SubArch = llvm::Triple::DXILSubArch_v1_3;
+break;
+  case 4:
+SubArch = llvm::Triple::DXILSubArch_v1_4;
+break;
+  case 5:
+SubArch = llvm::Triple::DXILSubArch_v1_5;
+break;
+  case 6:
+SubArch = llvm::Triple::DXILSubArch_v1_6;
+break;
+  case 7:
+SubArch = llvm::Triple::DXILSubArch_v1_7;
+break;
+  case 8:
+SubArch = llvm::Triple::DXILSubArch_v1_8;
+break;
+  case OfflineLibMinor:
+// Always consider minor version x as the latest supported DXIL version
+SubArch = llvm::Triple::LatestDXILSubArch;
+break;
+  default:
+// No DXIL Version corresponding to specified Shader Model version found
+return std::nullopt;
+  }
+  T.setArch(Triple::ArchType::dxil, SubArch);
   T.setOSName(Triple::getOSTypeName(Triple::OSType::ShaderModel).str() +
   VersionTuple(Major, Minor).getAsString());
   T.setEnvironment(Kind);
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 8312abc3603953..a2600174e02296 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -587,7 +587,8 @@ static bool FixupInvocation(CompilerInvocation &Invocation,
   LangOpts.SpeculativeLoadHardening = CodeGenOpts.SpeculativeLoadHardening;
   LangOpts.CurrentModule = LangOpts.ModuleName;
 
-  llvm::Triple T(TargetOpts.Triple);
+  llvm::Triple T(llvm::Triple::normalize(TargetOpts.Triple));
+
   llvm::Triple::ArchType Arch = T.getArch();
 
   CodeGenOpts.CodeModel = TargetOpts.CodeModel;
@@ -4675,7 +4676,7 @@ bool CompilerInvocation::CreateFromArgsImpl(
   // FIXME: We shouldn't have to pass the DashX option around here
   InputKind DashX = Res.getFrontendOpts().DashX;
   ParseTargetArgs(Res.getTargetOpts(), Args, Diags);
-  llvm::Triple T(Res.getTargetOpts().Triple);
+  llvm::Triple T(llvm::Triple::normalize(Res.getTargetOpts().Triple));
   ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), Args, Diags,
 Res.getFileSystemOpts().WorkingDir);
   ParseAPINotesArgs(Res.getAPINotesOpts(), Args, Diags);
@@ -4901,7 +4902,7 @@ std:

[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy updated 
https://github.com/llvm/llvm-project/pull/90809

>From 3b74e41492aeb916487105b05316f8db255c57c3 Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli 
Date: Wed, 1 May 2024 14:42:42 -0400
Subject: [PATCH 1/7] Set DXIL Version in DXIL target triple based on shader
 model version a) specified as compilation target profile or b) specified as
 target triple string

Update relevant HLSL tests that check for target triple.
---
 clang/lib/Basic/Targets.cpp   |  2 +-
 clang/lib/Driver/ToolChains/HLSL.cpp  | 44 +-
 clang/lib/Frontend/CompilerInvocation.cpp |  7 +--
 clang/test/CodeGenHLSL/basic-target.c |  2 +-
 clang/test/Driver/dxc_dxv_path.hlsl   |  6 +--
 .../enable_16bit_types_validation.hlsl|  4 +-
 clang/unittests/Driver/DXCModeTest.cpp| 22 -
 llvm/include/llvm/TargetParser/Triple.h   |  1 +
 llvm/lib/CodeGen/CommandFlags.cpp |  2 +-
 llvm/lib/IR/Verifier.cpp  |  4 +-
 llvm/lib/TargetParser/Triple.cpp  | 45 +++
 .../mir/infer-triple-unknown-target.mir   |  2 +-
 llvm/tools/opt/optdriver.cpp  |  2 +-
 13 files changed, 116 insertions(+), 27 deletions(-)

diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index e3283510c6aac7..dc1792b3471e6c 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -760,7 +760,7 @@ using namespace clang::targets;
 TargetInfo *
 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
  const std::shared_ptr &Opts) {
-  llvm::Triple Triple(Opts->Triple);
+  llvm::Triple Triple(llvm::Triple::normalize(Opts->Triple));
 
   // Construct the target
   std::unique_ptr Target = AllocateTarget(Triple, *Opts);
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp 
b/clang/lib/Driver/ToolChains/HLSL.cpp
index 558e4db46f8182..8286e3be21803f 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -98,9 +98,49 @@ std::optional tryParseProfile(StringRef 
Profile) {
   else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
 return std::nullopt;
 
-  // dxil-unknown-shadermodel-hull
+  // Determine DXIL version using the minor version number of Shader
+  // Model version specified in target profile. Prior to decoupling DXIL 
version
+  // numbering from that of Shader Model DXIL version 1.Y corresponds to SM 
6.Y.
+  // E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
   llvm::Triple T;
-  T.setArch(Triple::ArchType::dxil);
+  Triple::SubArchType SubArch = llvm::Triple::NoSubArch;
+  switch (Minor) {
+  case 0:
+SubArch = llvm::Triple::DXILSubArch_v1_0;
+break;
+  case 1:
+SubArch = llvm::Triple::DXILSubArch_v1_1;
+break;
+  case 2:
+SubArch = llvm::Triple::DXILSubArch_v1_2;
+break;
+  case 3:
+SubArch = llvm::Triple::DXILSubArch_v1_3;
+break;
+  case 4:
+SubArch = llvm::Triple::DXILSubArch_v1_4;
+break;
+  case 5:
+SubArch = llvm::Triple::DXILSubArch_v1_5;
+break;
+  case 6:
+SubArch = llvm::Triple::DXILSubArch_v1_6;
+break;
+  case 7:
+SubArch = llvm::Triple::DXILSubArch_v1_7;
+break;
+  case 8:
+SubArch = llvm::Triple::DXILSubArch_v1_8;
+break;
+  case OfflineLibMinor:
+// Always consider minor version x as the latest supported DXIL version
+SubArch = llvm::Triple::LatestDXILSubArch;
+break;
+  default:
+// No DXIL Version corresponding to specified Shader Model version found
+return std::nullopt;
+  }
+  T.setArch(Triple::ArchType::dxil, SubArch);
   T.setOSName(Triple::getOSTypeName(Triple::OSType::ShaderModel).str() +
   VersionTuple(Major, Minor).getAsString());
   T.setEnvironment(Kind);
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp 
b/clang/lib/Frontend/CompilerInvocation.cpp
index 8312abc3603953..a2600174e02296 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -587,7 +587,8 @@ static bool FixupInvocation(CompilerInvocation &Invocation,
   LangOpts.SpeculativeLoadHardening = CodeGenOpts.SpeculativeLoadHardening;
   LangOpts.CurrentModule = LangOpts.ModuleName;
 
-  llvm::Triple T(TargetOpts.Triple);
+  llvm::Triple T(llvm::Triple::normalize(TargetOpts.Triple));
+
   llvm::Triple::ArchType Arch = T.getArch();
 
   CodeGenOpts.CodeModel = TargetOpts.CodeModel;
@@ -4675,7 +4676,7 @@ bool CompilerInvocation::CreateFromArgsImpl(
   // FIXME: We shouldn't have to pass the DashX option around here
   InputKind DashX = Res.getFrontendOpts().DashX;
   ParseTargetArgs(Res.getTargetOpts(), Args, Diags);
-  llvm::Triple T(Res.getTargetOpts().Triple);
+  llvm::Triple T(llvm::Triple::normalize(Res.getTargetOpts().Triple));
   ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), Args, Diags,
 Res.getFileSystemOpts().WorkingDir);
   ParseAPINotesArgs(Res.getAPINotesOpts(), Args, Diags);
@@ -4901,7 +4902,7 @@ std:

[clang] [llvm] [DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #90809)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy closed 
https://github.com/llvm/llvm-project/pull/90809
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Set DXIL Version using shader model version in compilation target profile (PR #89823)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy closed 
https://github.com/llvm/llvm-project/pull/89823
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DirectX] Set DXIL Version using shader model version in compilation target profile (PR #89823)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits

bharadwajy wrote:

> Now the #90809 has landed, is this PR still relevant?

No. I was just about to close it. Thanks!

Closing as an expanded implementation has been merged.

https://github.com/llvm/llvm-project/pull/89823
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] Revert "[DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version" (PR #91290)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy created 
https://github.com/llvm/llvm-project/pull/91290

Reverts llvm/llvm-project#90809

Need to investigate ASAN failures.

>From 3be739599982371e6151561758928007d4dc2762 Mon Sep 17 00:00:00 2001
From: "S. Bharadwaj Yadavalli" 
Date: Mon, 6 May 2024 22:16:35 -0400
Subject: [PATCH] =?UTF-8?q?Revert=20"[DirectX][DXIL]=20Set=20DXIL=20Versio?=
 =?UTF-8?q?n=20in=20DXIL=20target=20triple=20based=20on=20shade=E2=80=A6"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This reverts commit 080978dd2067d0c9ea7e229aa7696c2480d89ef1.
---
 clang/lib/Basic/Targets.cpp   |  2 +-
 clang/lib/Driver/ToolChains/HLSL.cpp  | 44 +---
 clang/test/CodeGenHLSL/basic-target.c |  2 +-
 clang/test/Driver/dxc_dxv_path.hlsl   |  6 +-
 .../enable_16bit_types_validation.hlsl|  4 +-
 clang/unittests/Driver/DXCModeTest.cpp| 22 +++---
 llvm/include/llvm/TargetParser/Triple.h   |  1 -
 llvm/lib/IR/Verifier.cpp  |  4 +-
 llvm/lib/TargetParser/Triple.cpp  | 68 ---
 llvm/unittests/TargetParser/TripleTest.cpp| 16 -
 10 files changed, 21 insertions(+), 148 deletions(-)

diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index dc1792b3471e6c..e3283510c6aac7 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -760,7 +760,7 @@ using namespace clang::targets;
 TargetInfo *
 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
  const std::shared_ptr &Opts) {
-  llvm::Triple Triple(llvm::Triple::normalize(Opts->Triple));
+  llvm::Triple Triple(Opts->Triple);
 
   // Construct the target
   std::unique_ptr Target = AllocateTarget(Triple, *Opts);
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp 
b/clang/lib/Driver/ToolChains/HLSL.cpp
index 8286e3be21803f..558e4db46f8182 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -98,49 +98,9 @@ std::optional tryParseProfile(StringRef 
Profile) {
   else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
 return std::nullopt;
 
-  // Determine DXIL version using the minor version number of Shader
-  // Model version specified in target profile. Prior to decoupling DXIL 
version
-  // numbering from that of Shader Model DXIL version 1.Y corresponds to SM 
6.Y.
-  // E.g., dxilv1.Y-unknown-shadermodelX.Y-hull
+  // dxil-unknown-shadermodel-hull
   llvm::Triple T;
-  Triple::SubArchType SubArch = llvm::Triple::NoSubArch;
-  switch (Minor) {
-  case 0:
-SubArch = llvm::Triple::DXILSubArch_v1_0;
-break;
-  case 1:
-SubArch = llvm::Triple::DXILSubArch_v1_1;
-break;
-  case 2:
-SubArch = llvm::Triple::DXILSubArch_v1_2;
-break;
-  case 3:
-SubArch = llvm::Triple::DXILSubArch_v1_3;
-break;
-  case 4:
-SubArch = llvm::Triple::DXILSubArch_v1_4;
-break;
-  case 5:
-SubArch = llvm::Triple::DXILSubArch_v1_5;
-break;
-  case 6:
-SubArch = llvm::Triple::DXILSubArch_v1_6;
-break;
-  case 7:
-SubArch = llvm::Triple::DXILSubArch_v1_7;
-break;
-  case 8:
-SubArch = llvm::Triple::DXILSubArch_v1_8;
-break;
-  case OfflineLibMinor:
-// Always consider minor version x as the latest supported DXIL version
-SubArch = llvm::Triple::LatestDXILSubArch;
-break;
-  default:
-// No DXIL Version corresponding to specified Shader Model version found
-return std::nullopt;
-  }
-  T.setArch(Triple::ArchType::dxil, SubArch);
+  T.setArch(Triple::ArchType::dxil);
   T.setOSName(Triple::getOSTypeName(Triple::OSType::ShaderModel).str() +
   VersionTuple(Major, Minor).getAsString());
   T.setEnvironment(Kind);
diff --git a/clang/test/CodeGenHLSL/basic-target.c 
b/clang/test/CodeGenHLSL/basic-target.c
index b97ebf90a7a107..8db711c3f2a5b1 100644
--- a/clang/test/CodeGenHLSL/basic-target.c
+++ b/clang/test/CodeGenHLSL/basic-target.c
@@ -7,4 +7,4 @@
 // RUN: %clang -target dxil-pc-shadermodel6.0-geometry -S -emit-llvm -o - %s | 
FileCheck %s
 
 // CHECK: target datalayout = 
"e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64"
-// CHECK: target triple = "dxilv1.0-pc-shadermodel6.0-{{[a-z]+}}"
+// CHECK: target triple = "dxil-pc-shadermodel6.0-{{[a-z]+}}"
diff --git a/clang/test/Driver/dxc_dxv_path.hlsl 
b/clang/test/Driver/dxc_dxv_path.hlsl
index 4845de11d5b00b..3d8e90d0d91975 100644
--- a/clang/test/Driver/dxc_dxv_path.hlsl
+++ b/clang/test/Driver/dxc_dxv_path.hlsl
@@ -7,12 +7,12 @@
 // DXV_PATH:dxv{{(.exe)?}}" "-" "-o" "-"
 
 // RUN: %clang_dxc -I test -Vd -Tlib_6_3  -### %s 2>&1 | FileCheck %s 
--check-prefix=VD
-// VD:"-cc1"{{.*}}"-triple" "dxilv1.3-unknown-shadermodel6.3-library"
+// VD:"-cc1"{{.*}}"-triple" "dxil-unknown-shadermodel6.3-library"
 // VD-NOT:dxv not found
 
 // RUN: %clang_dxc -Tlib_6_3 -ccc-print-bindings --dxv-path=%T -Fo %t.dxo  %s 
2>&1 | FileCheck %s --check-prefix=BINDINGS
-// BIN

[clang] [llvm] Revert "[DirectX][DXIL] Set DXIL Version in DXIL target triple based on shader model version" (PR #91290)

2024-05-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy closed 
https://github.com/llvm/llvm-project/pull/91290
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #91407)

2024-05-07 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy created 
https://github.com/llvm/llvm-project/pull/91407

This change set restores the PR #90809 that was reverted to address ASAN 
failures and includes a fix for the ASAN failures. 

Following is the description of the change:

An earlier commit provided a way to decouple DXIL version from Shader Model 
version
by representing the DXIL version as `SubArch` in the DXIL Target Triple and 
adding 
corresponding valid DXIL Arch types.

This change constructs DXIL target triple with DXIL version that is deduced
from Shader Model version specified in the following scenarios:

1. When compilation target profile is specified: 
   For e.g., DXIL target triple `dxilv1.8-unknown-shader6.8-library` is 
constructed 
   when `-T lib_6_8` is specified. 
2. When DXIL target triple without DXIL version is specified:
For e.g., DXIL target triple `dxilv1.8-pc-shadermodel6.8-library` is 
constructed
when `-mtriple=dxil-pc-shadermodel6.8-library` is specified.

Note that this is an alternate / expanded implementation to that proposed in PR 
#89823.

PR #89823 implements functionality (1) above. However, it requires any DXIL 
target  
triple to explicitly include DXIL version anywhere DXIL target triple is 
expected (e.g., 
`dxilv1.8-pc-shadermodel6.8-library`) while a triple without DXIL version 
(e.g., `dxil-pc-shadermodel6.8-library`) is considered invalid. This 
functionality is
implemented as a change to `tryParseProfile()` and is included in this PR.

This PR adds the functionality (2) to eliminate the above requirement to 
explicitly 
specify DXIL version in the target triple thereby considering a triple without 
DXIL 
version ( e.g., `dxil-pc-shadermodel6.8-library`) to be valid. A triple with 
DXIL version 
is inferred based on the shader mode version specified. If no shader model 
version is 
specified, DXIL version is defaulted to 1.0. This functionality is implemented 
in the 
"Special case logic ..." section of `Triple::normalize()`. 
 
Updated relevant HLSL tests that check for target triple. 

Validated that Clang (`check-clang`), LLVM (`check-llvm`) regression tests and 
`TargetParserTests` built with sanitizer, pass. 

>From 78f9aa5849b30ee480221f09f2257faa6d6e6eed Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli 
Date: Tue, 7 May 2024 18:32:44 +
Subject: [PATCH 1/2] Restore "[DirectX][DXIL] Set DXIL Version in DXIL target
 triple based on shader model version" (#91290)"

This restores commit 178ff395006f204265b4f6fe72a3dbb2b9a79b47.

An earlier commit provided a way to decouple DXIL version from Shader
Model version by representing the DXIL version as `SubArch` in the DXIL
Target Triple and adding corresponding valid DXIL Arch types.

This change constructs DXIL target triple with DXIL version that is
deduced from Shader Model version specified in the following scenarios:

 1. When compilation target profile is specified:
For e.g., DXIL target triple `dxilv1.8-unknown-shader6.8-library` is
constructed when `-T lib_6_8` is specified.
 2. When DXIL target triple without DXIL version is specified:
For e.g., DXIL target triple `dxilv1.8-pc-shadermodel6.8-library` is
constructed when `-mtriple=dxil-pc-shadermodel6.8-library` is specified.

Updated relevant HLSL tests that check for target triple.
---
 clang/lib/Basic/Targets.cpp   |  2 +-
 clang/lib/Driver/ToolChains/HLSL.cpp  | 44 +++-
 clang/test/CodeGenHLSL/basic-target.c |  2 +-
 clang/test/Driver/dxc_dxv_path.hlsl   |  6 +-
 .../enable_16bit_types_validation.hlsl|  4 +-
 clang/unittests/Driver/DXCModeTest.cpp| 22 +++---
 llvm/include/llvm/TargetParser/Triple.h   |  1 +
 llvm/lib/IR/Verifier.cpp  |  4 +-
 llvm/lib/TargetParser/Triple.cpp  | 68 +++
 llvm/unittests/TargetParser/TripleTest.cpp| 16 +
 10 files changed, 148 insertions(+), 21 deletions(-)

diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index e3283510c6aac..dc1792b3471e6 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -760,7 +760,7 @@ using namespace clang::targets;
 TargetInfo *
 TargetInfo::CreateTargetInfo(DiagnosticsEngine &Diags,
  const std::shared_ptr &Opts) {
-  llvm::Triple Triple(Opts->Triple);
+  llvm::Triple Triple(llvm::Triple::normalize(Opts->Triple));
 
   // Construct the target
   std::unique_ptr Target = AllocateTarget(Triple, *Opts);
diff --git a/clang/lib/Driver/ToolChains/HLSL.cpp 
b/clang/lib/Driver/ToolChains/HLSL.cpp
index 558e4db46f818..8286e3be21803 100644
--- a/clang/lib/Driver/ToolChains/HLSL.cpp
+++ b/clang/lib/Driver/ToolChains/HLSL.cpp
@@ -98,9 +98,49 @@ std::optional tryParseProfile(StringRef 
Profile) {
   else if (llvm::getAsUnsignedInteger(Parts[2], 0, Minor))
 return std::nullopt;
 
-  // dxil-unknown-shadermodel-hull
+  // Determine DXIL version using the minor version number of Shader
+  // M

[clang] [llvm] [DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #91407)

2024-05-08 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy edited 
https://github.com/llvm/llvm-project/pull/91407
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #91407)

2024-05-08 Thread S. Bharadwaj Yadavalli via cfe-commits

bharadwajy wrote:

> It would be nice to mention in the description what the sanitizer fix was 
> compared to the original PR.

Thanks! The fix to address ASAN failure is in commit 
8522e3609869d72b7b457a4752149a802be4662d of this PR. Updated the description as 
well.

https://github.com/llvm/llvm-project/pull/91407
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [DXIL] Set DXIL Version in DXIL target triple based on shader model version (PR #91407)

2024-05-08 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy closed 
https://github.com/llvm/llvm-project/pull/91407
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL] Implement support for HLSL intrinsic - saturate (PR #104619)

2024-08-19 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -362,6 +364,34 @@ static bool expandClampIntrinsic(CallInst *Orig, 
Intrinsic::ID ClampIntrinsic) {
   return true;
 }
 
+static bool expandSaturateIntrinsic(CallInst *SaturateCall) {
+  FunctionType *FT = SaturateCall->getFunctionType();
+  Type *FTRetTy = FT->getReturnType();
+  assert(FTRetTy == FT->getParamType(0) &&

bharadwajy wrote:

> It looks like you are trying to do scalarization via this pass, but we 
> haven't been doing that thus far. I wanted to take care of scalarization in a 
> different pass because doing it here means we have to do it for each 
> intrinsic which I want to avoid. My feeling is scalarization should be 
> uniform across all intrinsics unless there are special cases like `dot2`, 
> `dot3`, `dot4`.

The functionality can be moved out of here once such a scalarization pass is 
implemented.

https://github.com/llvm/llvm-project/pull/104619
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL] Implement support for HLSL intrinsic - saturate (PR #104619)

2024-08-19 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -1285,6 +1289,34 @@ static unsigned getBoolCmpOpcode(unsigned PredNum) {
   }
 }
 
+static APFloat getZeroFP(const Type *LLVMFloatTy) {

bharadwajy wrote:

> Why did this move?

The intent was to co-locate it with the other `static` functions in the 
vicinity while adding the new one `getOneFP`.

https://github.com/llvm/llvm-project/pull/104619
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL] Implement support for HLSL intrinsic - saturate (PR #104619)

2024-08-19 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy updated 
https://github.com/llvm/llvm-project/pull/104619

>From 7f338f8843ed815641541664ce2eff72278aacab Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli 
Date: Thu, 1 Aug 2024 02:46:05 +
Subject: [PATCH 1/2] Implement support to compile HLSL intrinsic "saturate" to
 DXIL

Add SPIRV Codegen support to transform saturate(x) to clamp(x, 0.0, 1.0)
Add tests for DXIL and SPIRV CodeGen.
---
 clang/include/clang/Basic/Builtins.td |   6 +
 clang/lib/CodeGen/CGBuiltin.cpp   |   9 +
 clang/lib/CodeGen/CGHLSLRuntime.h |   1 +
 clang/lib/Headers/hlsl/hlsl_intrinsics.h  |  41 ++-
 clang/lib/Sema/SemaHLSL.cpp   |   3 +-
 clang/test/CodeGenHLSL/builtins/saturate.hlsl |  54 
 .../SemaHLSL/BuiltIns/saturate-errors.hlsl|  31 ++
 llvm/include/llvm/IR/IntrinsicsDirectX.td |   1 +
 llvm/include/llvm/IR/IntrinsicsSPIRV.td   |   3 +-
 llvm/lib/Target/DirectX/DXIL.td   |  10 +
 .../Target/DirectX/DXILIntrinsicExpansion.cpp |  32 ++
 .../Target/SPIRV/SPIRVInstructionSelector.cpp |  80 -
 llvm/test/CodeGen/DirectX/saturate.ll | 276 ++
 llvm/test/CodeGen/DirectX/saturate_errors.ll  |  14 +
 .../CodeGen/SPIRV/hlsl-intrinsics/saturate.ll |  83 ++
 15 files changed, 627 insertions(+), 17 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/builtins/saturate.hlsl
 create mode 100644 clang/test/SemaHLSL/BuiltIns/saturate-errors.hlsl
 create mode 100644 llvm/test/CodeGen/DirectX/saturate.ll
 create mode 100644 llvm/test/CodeGen/DirectX/saturate_errors.ll
 create mode 100644 llvm/test/CodeGen/SPIRV/hlsl-intrinsics/saturate.ll

diff --git a/clang/include/clang/Basic/Builtins.td 
b/clang/include/clang/Basic/Builtins.td
index 0a874d8638df43..76e893e38b671c 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4743,6 +4743,12 @@ def HLSLRSqrt : LangBuiltin<"HLSL_LANG"> {
   let Prototype = "void(...)";
 }
 
+def HLSLSaturate : LangBuiltin<"HLSL_LANG"> {
+  let Spellings = ["__builtin_hlsl_elementwise_saturate"];
+  let Attributes = [NoThrow, Const];
+  let Prototype = "void(...)";
+}
+
 // Builtins for XRay.
 def XRayCustomEvent : Builtin {
   let Spellings = ["__xray_customevent"];
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 1c0baeaee03632..01841774562f06 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -18666,6 +18666,15 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
 /*ReturnType=*/Op0->getType(), 
CGM.getHLSLRuntime().getRsqrtIntrinsic(),
 ArrayRef{Op0}, nullptr, "hlsl.rsqrt");
   }
+  case Builtin::BI__builtin_hlsl_elementwise_saturate: {
+Value *Op0 = EmitScalarExpr(E->getArg(0));
+if (!E->getArg(0)->getType()->hasFloatingRepresentation())
+  llvm_unreachable("saturate operand must have a float representation");
+return Builder.CreateIntrinsic(
+/*ReturnType=*/Op0->getType(),
+CGM.getHLSLRuntime().getSaturateIntrinsic(), ArrayRef{Op0},
+nullptr, "hlsl.saturate");
+  }
   case Builtin::BI__builtin_hlsl_wave_get_lane_index: {
 return EmitRuntimeCall(CGM.CreateRuntimeFunction(
 llvm::FunctionType::get(IntTy, {}, false), 
"__hlsl_wave_get_lane_index",
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h 
b/clang/lib/CodeGen/CGHLSLRuntime.h
index cd604bea2e763d..b1455b5779acf9 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.h
+++ b/clang/lib/CodeGen/CGHLSLRuntime.h
@@ -79,6 +79,7 @@ class CGHLSLRuntime {
   GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp)
   GENERATE_HLSL_INTRINSIC_FUNCTION(Normalize, normalize)
   GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt)
+  GENERATE_HLSL_INTRINSIC_FUNCTION(Saturate, saturate)
   GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
 
   
//===--===//
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h 
b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index 678cdc77f8a71b..6d38b668fe770e 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -916,7 +916,7 @@ float4 lerp(float4, float4, float4);
 /// \brief Returns the length of the specified floating-point vector.
 /// \param x [in] The vector of floats, or a scalar float.
 ///
-/// Length is based on the following formula: sqrt(x[0]^2 + x[1]^2 + �).
+/// Length is based on the following formula: sqrt(x[0]^2 + x[1]^2 + ...).
 
 _HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
 _HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
@@ -1564,6 +1564,45 @@ float3 round(float3);
 _HLSL_BUILTIN_ALIAS(__builtin_elementwise_roundeven)
 float4 round(float4);
 
+//===--===//
+// saturate builtins
+//===--===//
+
+/// \fn T saturate(T Val)
+/// \brief Returns input value, \a Val, clamped within the rang

[clang] [llvm] [HLSL] Implement support for HLSL intrinsic - saturate (PR #104619)

2024-08-19 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -18666,6 +18666,15 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
 /*ReturnType=*/Op0->getType(), 
CGM.getHLSLRuntime().getRsqrtIntrinsic(),
 ArrayRef{Op0}, nullptr, "hlsl.rsqrt");
   }
+  case Builtin::BI__builtin_hlsl_elementwise_saturate: {
+Value *Op0 = EmitScalarExpr(E->getArg(0));
+if (!E->getArg(0)->getType()->hasFloatingRepresentation())

bharadwajy wrote:

> you already handle this case in `clang/lib/Sema/SemaHLSL.cpp` this should 
> just be an assert.

Changed.

https://github.com/llvm/llvm-project/pull/104619
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL] Implement support for HLSL intrinsic - saturate (PR #104619)

2024-08-19 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -0,0 +1,54 @@
+// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
+// RUN:   dxil-pc-shadermodel6.3-library %s -fnative-half-type \
+// RUN:   -emit-llvm -disable-llvm-passes -o - | FileCheck %s \
+// RUN:   --check-prefixes=CHECK,NATIVE_HALF
+// RUN: %clang_cc1 -std=hlsl2021 -finclude-default-header -x hlsl -triple \
+// RUN:   dxil-pc-shadermodel6.3-library %s -emit-llvm -disable-llvm-passes \
+// RUN:   -o - | FileCheck %s --check-prefixes=CHECK,NO_HALF

bharadwajy wrote:

> add spirv tests so you can check for `@llvm.spv.saturate`

Done.

https://github.com/llvm/llvm-project/pull/104619
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL] Implement support for HLSL intrinsic - saturate (PR #104619)

2024-08-19 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy updated 
https://github.com/llvm/llvm-project/pull/104619

>From e9fd01be8d43b2eb7ccd4894022ecec4c596d957 Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli 
Date: Thu, 1 Aug 2024 02:46:05 +
Subject: [PATCH 1/3] Implement support to compile HLSL intrinsic "saturate" to
 DXIL

Add SPIRV Codegen support to transform saturate(x) to clamp(x, 0.0, 1.0)
Add tests for DXIL and SPIRV CodeGen.
---
 clang/include/clang/Basic/Builtins.td |   6 +
 clang/lib/CodeGen/CGBuiltin.cpp   |   9 +
 clang/lib/CodeGen/CGHLSLRuntime.h |   1 +
 clang/lib/Headers/hlsl/hlsl_intrinsics.h  |  41 ++-
 clang/lib/Sema/SemaHLSL.cpp   |   3 +-
 clang/test/CodeGenHLSL/builtins/saturate.hlsl |  54 
 .../SemaHLSL/BuiltIns/saturate-errors.hlsl|  31 ++
 llvm/include/llvm/IR/IntrinsicsDirectX.td |   1 +
 llvm/include/llvm/IR/IntrinsicsSPIRV.td   |   3 +-
 llvm/lib/Target/DirectX/DXIL.td   |  10 +
 .../Target/DirectX/DXILIntrinsicExpansion.cpp |   2 +
 .../Target/SPIRV/SPIRVInstructionSelector.cpp |  80 -
 llvm/test/CodeGen/DirectX/saturate.ll | 276 ++
 llvm/test/CodeGen/DirectX/saturate_errors.ll  |  14 +
 .../CodeGen/SPIRV/hlsl-intrinsics/saturate.ll |  83 ++
 15 files changed, 597 insertions(+), 17 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/builtins/saturate.hlsl
 create mode 100644 clang/test/SemaHLSL/BuiltIns/saturate-errors.hlsl
 create mode 100644 llvm/test/CodeGen/DirectX/saturate.ll
 create mode 100644 llvm/test/CodeGen/DirectX/saturate_errors.ll
 create mode 100644 llvm/test/CodeGen/SPIRV/hlsl-intrinsics/saturate.ll

diff --git a/clang/include/clang/Basic/Builtins.td 
b/clang/include/clang/Basic/Builtins.td
index 036366cdadf4aa..ac33672a32b336 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4745,6 +4745,12 @@ def HLSLRSqrt : LangBuiltin<"HLSL_LANG"> {
   let Prototype = "void(...)";
 }
 
+def HLSLSaturate : LangBuiltin<"HLSL_LANG"> {
+  let Spellings = ["__builtin_hlsl_elementwise_saturate"];
+  let Attributes = [NoThrow, Const];
+  let Prototype = "void(...)";
+}
+
 // Builtins for XRay.
 def XRayCustomEvent : Builtin {
   let Spellings = ["__xray_customevent"];
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index f424ddaa175400..f4353f595efbac 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -18667,6 +18667,15 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
 /*ReturnType=*/Op0->getType(), 
CGM.getHLSLRuntime().getRsqrtIntrinsic(),
 ArrayRef{Op0}, nullptr, "hlsl.rsqrt");
   }
+  case Builtin::BI__builtin_hlsl_elementwise_saturate: {
+Value *Op0 = EmitScalarExpr(E->getArg(0));
+if (!E->getArg(0)->getType()->hasFloatingRepresentation())
+  llvm_unreachable("saturate operand must have a float representation");
+return Builder.CreateIntrinsic(
+/*ReturnType=*/Op0->getType(),
+CGM.getHLSLRuntime().getSaturateIntrinsic(), ArrayRef{Op0},
+nullptr, "hlsl.saturate");
+  }
   case Builtin::BI__builtin_hlsl_wave_get_lane_index: {
 return EmitRuntimeCall(CGM.CreateRuntimeFunction(
 llvm::FunctionType::get(IntTy, {}, false), 
"__hlsl_wave_get_lane_index",
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h 
b/clang/lib/CodeGen/CGHLSLRuntime.h
index cd604bea2e763d..b1455b5779acf9 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.h
+++ b/clang/lib/CodeGen/CGHLSLRuntime.h
@@ -79,6 +79,7 @@ class CGHLSLRuntime {
   GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp)
   GENERATE_HLSL_INTRINSIC_FUNCTION(Normalize, normalize)
   GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt)
+  GENERATE_HLSL_INTRINSIC_FUNCTION(Saturate, saturate)
   GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
 
   
//===--===//
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h 
b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index 678cdc77f8a71b..6d38b668fe770e 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -916,7 +916,7 @@ float4 lerp(float4, float4, float4);
 /// \brief Returns the length of the specified floating-point vector.
 /// \param x [in] The vector of floats, or a scalar float.
 ///
-/// Length is based on the following formula: sqrt(x[0]^2 + x[1]^2 + �).
+/// Length is based on the following formula: sqrt(x[0]^2 + x[1]^2 + ...).
 
 _HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
 _HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
@@ -1564,6 +1564,45 @@ float3 round(float3);
 _HLSL_BUILTIN_ALIAS(__builtin_elementwise_roundeven)
 float4 round(float4);
 
+//===--===//
+// saturate builtins
+//===--===//
+
+/// \fn T saturate(T Val)
+/// \brief Returns input value, \a Val, clamped within the range

[clang] [llvm] [HLSL] Implement support for HLSL intrinsic - saturate (PR #104619)

2024-08-19 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -362,6 +364,34 @@ static bool expandClampIntrinsic(CallInst *Orig, 
Intrinsic::ID ClampIntrinsic) {
   return true;
 }
 
+static bool expandSaturateIntrinsic(CallInst *SaturateCall) {
+  FunctionType *FT = SaturateCall->getFunctionType();
+  Type *FTRetTy = FT->getReturnType();
+  assert(FTRetTy == FT->getParamType(0) &&

bharadwajy wrote:

> I think we should drop this for now. We likely won't be preserving any 
> intrinsic specific behaviors.

Pushed a change dropping scalarization functionality and associated tests.

https://github.com/llvm/llvm-project/pull/104619
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [HLSL] Implement support for HLSL intrinsic - saturate (PR #104619)

2024-08-19 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy updated 
https://github.com/llvm/llvm-project/pull/104619

>From e9fd01be8d43b2eb7ccd4894022ecec4c596d957 Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli 
Date: Thu, 1 Aug 2024 02:46:05 +
Subject: [PATCH 1/4] Implement support to compile HLSL intrinsic "saturate" to
 DXIL

Add SPIRV Codegen support to transform saturate(x) to clamp(x, 0.0, 1.0)
Add tests for DXIL and SPIRV CodeGen.
---
 clang/include/clang/Basic/Builtins.td |   6 +
 clang/lib/CodeGen/CGBuiltin.cpp   |   9 +
 clang/lib/CodeGen/CGHLSLRuntime.h |   1 +
 clang/lib/Headers/hlsl/hlsl_intrinsics.h  |  41 ++-
 clang/lib/Sema/SemaHLSL.cpp   |   3 +-
 clang/test/CodeGenHLSL/builtins/saturate.hlsl |  54 
 .../SemaHLSL/BuiltIns/saturate-errors.hlsl|  31 ++
 llvm/include/llvm/IR/IntrinsicsDirectX.td |   1 +
 llvm/include/llvm/IR/IntrinsicsSPIRV.td   |   3 +-
 llvm/lib/Target/DirectX/DXIL.td   |  10 +
 .../Target/DirectX/DXILIntrinsicExpansion.cpp |   2 +
 .../Target/SPIRV/SPIRVInstructionSelector.cpp |  80 -
 llvm/test/CodeGen/DirectX/saturate.ll | 276 ++
 llvm/test/CodeGen/DirectX/saturate_errors.ll  |  14 +
 .../CodeGen/SPIRV/hlsl-intrinsics/saturate.ll |  83 ++
 15 files changed, 597 insertions(+), 17 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/builtins/saturate.hlsl
 create mode 100644 clang/test/SemaHLSL/BuiltIns/saturate-errors.hlsl
 create mode 100644 llvm/test/CodeGen/DirectX/saturate.ll
 create mode 100644 llvm/test/CodeGen/DirectX/saturate_errors.ll
 create mode 100644 llvm/test/CodeGen/SPIRV/hlsl-intrinsics/saturate.ll

diff --git a/clang/include/clang/Basic/Builtins.td 
b/clang/include/clang/Basic/Builtins.td
index 036366cdadf4aa..ac33672a32b336 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4745,6 +4745,12 @@ def HLSLRSqrt : LangBuiltin<"HLSL_LANG"> {
   let Prototype = "void(...)";
 }
 
+def HLSLSaturate : LangBuiltin<"HLSL_LANG"> {
+  let Spellings = ["__builtin_hlsl_elementwise_saturate"];
+  let Attributes = [NoThrow, Const];
+  let Prototype = "void(...)";
+}
+
 // Builtins for XRay.
 def XRayCustomEvent : Builtin {
   let Spellings = ["__xray_customevent"];
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index f424ddaa175400..f4353f595efbac 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -18667,6 +18667,15 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
 /*ReturnType=*/Op0->getType(), 
CGM.getHLSLRuntime().getRsqrtIntrinsic(),
 ArrayRef{Op0}, nullptr, "hlsl.rsqrt");
   }
+  case Builtin::BI__builtin_hlsl_elementwise_saturate: {
+Value *Op0 = EmitScalarExpr(E->getArg(0));
+if (!E->getArg(0)->getType()->hasFloatingRepresentation())
+  llvm_unreachable("saturate operand must have a float representation");
+return Builder.CreateIntrinsic(
+/*ReturnType=*/Op0->getType(),
+CGM.getHLSLRuntime().getSaturateIntrinsic(), ArrayRef{Op0},
+nullptr, "hlsl.saturate");
+  }
   case Builtin::BI__builtin_hlsl_wave_get_lane_index: {
 return EmitRuntimeCall(CGM.CreateRuntimeFunction(
 llvm::FunctionType::get(IntTy, {}, false), 
"__hlsl_wave_get_lane_index",
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h 
b/clang/lib/CodeGen/CGHLSLRuntime.h
index cd604bea2e763d..b1455b5779acf9 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.h
+++ b/clang/lib/CodeGen/CGHLSLRuntime.h
@@ -79,6 +79,7 @@ class CGHLSLRuntime {
   GENERATE_HLSL_INTRINSIC_FUNCTION(Lerp, lerp)
   GENERATE_HLSL_INTRINSIC_FUNCTION(Normalize, normalize)
   GENERATE_HLSL_INTRINSIC_FUNCTION(Rsqrt, rsqrt)
+  GENERATE_HLSL_INTRINSIC_FUNCTION(Saturate, saturate)
   GENERATE_HLSL_INTRINSIC_FUNCTION(ThreadId, thread_id)
 
   
//===--===//
diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h 
b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
index 678cdc77f8a71b..6d38b668fe770e 100644
--- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h
+++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h
@@ -916,7 +916,7 @@ float4 lerp(float4, float4, float4);
 /// \brief Returns the length of the specified floating-point vector.
 /// \param x [in] The vector of floats, or a scalar float.
 ///
-/// Length is based on the following formula: sqrt(x[0]^2 + x[1]^2 + �).
+/// Length is based on the following formula: sqrt(x[0]^2 + x[1]^2 + ...).
 
 _HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
 _HLSL_BUILTIN_ALIAS(__builtin_hlsl_length)
@@ -1564,6 +1564,45 @@ float3 round(float3);
 _HLSL_BUILTIN_ALIAS(__builtin_elementwise_roundeven)
 float4 round(float4);
 
+//===--===//
+// saturate builtins
+//===--===//
+
+/// \fn T saturate(T Val)
+/// \brief Returns input value, \a Val, clamped within the range

[clang] [llvm] [HLSL] Implement support for HLSL intrinsic - saturate (PR #104619)

2024-08-20 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy closed 
https://github.com/llvm/llvm-project/pull/104619
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Switch DirectX Target to use the Itanium ABI (PR #111632)

2024-10-09 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy approved this pull request.


https://github.com/llvm/llvm-project/pull/111632
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Appropriately set function attribute optnone (PR #125937)

2025-02-10 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy updated 
https://github.com/llvm/llvm-project/pull/125937

>From 63a728dd7f5efff32a2f90608adfdc13d540f34f Mon Sep 17 00:00:00 2001
From: "S. Bharadwaj Yadavalli" 
Date: Tue, 4 Feb 2025 12:48:09 -0500
Subject: [PATCH 1/2] [HLSL] Set function optnone attribute appropriately

When optimization is disabled, set optnone attribute
  - for all module functions when targetting Library shaders
  - only for entry function when targetting non-Library shaders

Update tests in accordance with the change.
---
 clang/lib/CodeGen/CGHLSLRuntime.cpp   |  10 +
 .../CodeGenHLSL/GlobalConstructorLib.hlsl |   8 +-
 clang/test/CodeGenHLSL/GlobalDestructors.hlsl |   4 +-
 .../test/CodeGenHLSL/inline-constructors.hlsl |  16 +-
 clang/test/CodeGenHLSL/inline-functions.hlsl  | 189 +-
 .../CodeGenHLSL/this-assignment-overload.hlsl |   6 +-
 6 files changed, 174 insertions(+), 59 deletions(-)

diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 2ce54cc3c52efa0..24acc9a559be2df 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -345,6 +345,9 @@ void clang::CodeGen::CGHLSLRuntime::setHLSLEntryAttributes(
 WaveSizeAttr->getPreferred());
 Fn->addFnAttr(WaveSizeKindStr, WaveSizeStr);
   }
+  if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
+Fn->addFnAttr(llvm::Attribute::OptimizeNone);
+  }
   Fn->addFnAttr(llvm::Attribute::NoInline);
 }
 
@@ -446,6 +449,13 @@ void CGHLSLRuntime::setHLSLFunctionAttributes(const 
FunctionDecl *FD,
 const StringRef ExportAttrKindStr = "hlsl.export";
 Fn->addFnAttr(ExportAttrKindStr);
   }
+  llvm::Triple T(Fn->getParent()->getTargetTriple());
+  if (T.getEnvironment() == llvm::Triple::EnvironmentType::Library) {
+if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
+  Fn->addFnAttr(llvm::Attribute::OptimizeNone);
+  Fn->addFnAttr(llvm::Attribute::NoInline);
+}
+  }
 }
 
 static void gatherFunctions(SmallVectorImpl &Fns, llvm::Module &M,
diff --git a/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl 
b/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl
index 09c44f6242c53c7..39d7c73e832a104 100644
--- a/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl
+++ b/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl
@@ -1,5 +1,5 @@
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm 
-disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=CHECK,NOINLINE
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -O0 %s -o 
- | FileCheck %s --check-prefixes=CHECK,INLINE
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -O1 %s -o 
- | FileCheck %s --check-prefixes=CHECK,INLINE
 
 // Make sure global variable for ctors exist for lib profile.
 // CHECK:@llvm.global_ctors
@@ -31,12 +31,12 @@ void SecondEntry() {}
 // CHECK: ret void
 
 
-// Verify the constructor is alwaysinline
-// NOINLINE: ; Function Attrs: {{.*}}alwaysinline
+// Verify the constructor is optnone
+// NOINLINE: ; Function Attrs: {{.*}} optnone
 // NOINLINE-NEXT: define linkonce_odr void @_ZN4hlsl8RWBufferIfEC2Ev({{.*}} 
[[CtorAttr:\#[0-9]+]]
 
 // NOINLINE: ; Function Attrs: {{.*}}alwaysinline
 // NOINLINE-NEXT: define internal void 
@_GLOBAL__sub_I_GlobalConstructorLib.hlsl() [[InitAttr:\#[0-9]+]]
 
 // NOINLINE-DAG: attributes [[InitAttr]] = {{.*}} alwaysinline
-// NOINLINE-DAG: attributes [[CtorAttr]] = {{.*}} alwaysinline
+// NOINLINE-DAG: attributes [[CtorAttr]] = {{.*}} optnone
diff --git a/clang/test/CodeGenHLSL/GlobalDestructors.hlsl 
b/clang/test/CodeGenHLSL/GlobalDestructors.hlsl
index f98318601134bb2..8961e1a7e59cd9c 100644
--- a/clang/test/CodeGenHLSL/GlobalDestructors.hlsl
+++ b/clang/test/CodeGenHLSL/GlobalDestructors.hlsl
@@ -1,7 +1,7 @@
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -emit-llvm 
-disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=CS,NOINLINE,CHECK
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm 
-disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=LIB,NOINLINE,CHECK
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -emit-llvm -O0 %s -o 
- | FileCheck %s --check-prefixes=INLINE,CHECK
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -O0 %s -o 
- | FileCheck %s --check-prefixes=INLINE,CHECK
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -emit-llvm -O1 %s -o 
- | FileCheck %s --check-prefixes=INLINE,CHECK
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -O1 %s -o 
- | FileCheck %s --check-prefixes=INLINE,CHECK
 
 // Tests that constructors and destructors are appropriately generated for 
globals
 // and that their calls are inlined when AlwaysInline is run
diff --git a/clang/test/CodeGenHLSL/inline-constructors.hlsl 
b/clang/test/CodeGenHLSL/inline-constructors.hlsl
index b0d5a783fb3725c..298d7d42726789c 100644
--- a/clang/test/CodeGenHLSL/inline-constructors.hlsl
+++ b/clang/tes

[clang] [HLSL] Appropriately set function attribute optnone (PR #125937)

2025-02-10 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -345,6 +345,9 @@ void clang::CodeGen::CGHLSLRuntime::setHLSLEntryAttributes(
 WaveSizeAttr->getPreferred());
 Fn->addFnAttr(WaveSizeKindStr, WaveSizeStr);
   }
+  if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
+Fn->addFnAttr(llvm::Attribute::OptimizeNone);
+  }
   Fn->addFnAttr(llvm::Attribute::NoInline);

bharadwajy wrote:

> Given that these already have "noinline", I'm surprised that the logic in 
> "SetLLVMFunctionAttributesForDefinition" doesn't already put optnone on these 
> functions. Is something undoing this later?

The entry function that is created and whose attribute is set to `noinline` in 
this function is different from that `SetLLVMFunctionAttributesForDefinition()` 
looks at.

`GenerateCode(GlobalFnDecl, MangledFn, ...)` calls `StartFunction(GlobalFnDecl, 
ResTy, MangledFn, ...)` which in turn calls `emitEntryFunction(FnDecl, 
MangledFn)`. `emitEntryFunction(FnDecl, MangledFn, ...)` constructs a new entry 
function `EntryFn`, sets linkage of `MangledFn` to be `internal` to arrange it 
to be inlined in `EntryFn` etc., and calls `setHLSLEntryAttributes(FnDecl, 
EntryFn)` to set attributes of `EntryFn`.

`SetLLVMFunctionAttributesForDefinition(...)` sets attributes of `MangledFn`. 
So the logic in that function checks attributes for `MangledFn` and not for the 
created `EntryFn`.

Hence setting optnone attribute in `setHLSLEntryAttributes(FnDecl, EntryFn)` of 
`EntryFn` at the time of its set up seemed appropriate - if optimizations are 
disabled.


https://github.com/llvm/llvm-project/pull/125937
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Appropriately set function attribute optnone (PR #125937)

2025-02-10 Thread S. Bharadwaj Yadavalli via cfe-commits


@@ -446,6 +449,13 @@ void CGHLSLRuntime::setHLSLFunctionAttributes(const 
FunctionDecl *FD,
 const StringRef ExportAttrKindStr = "hlsl.export";
 Fn->addFnAttr(ExportAttrKindStr);
   }
+  llvm::Triple T(Fn->getParent()->getTargetTriple());
+  if (T.getEnvironment() == llvm::Triple::EnvironmentType::Library) {
+if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
+  Fn->addFnAttr(llvm::Attribute::OptimizeNone);
+  Fn->addFnAttr(llvm::Attribute::NoInline);
+}
+  }

bharadwajy wrote:

> Do we want to do this on all functions in a library or just entry points and 
> exported functions? In any case, it really would be preferable if 
> "SetLLVMFunctionAttributesForDefinition" did the right thing (whatever that 
> may be) rather than us needing to duplicate that logic here...

OK. It would be sufficient to set the `optnone` attribute just for entry 
functions of both non-library shaders and library shaders since all shaders 
will have one or more (respectively) entry functions. The shader flag 
`DisableOptimizations` can be set based on the presence of this attribute on 
entry function(s).

Deleted this change.

The consequences and utility in the later passes of setting `optnone` attribute 
for exported library functions when optimizatons are disabled is not very clear 
to me, yet. I'd like to propose an change, if needed, be done in a follow on 
PR. 

https://github.com/llvm/llvm-project/pull/125937
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Appropriately set function attribute optnone (PR #125937)

2025-02-10 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy edited 
https://github.com/llvm/llvm-project/pull/125937
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Appropriately set function attribute optnone (PR #125937)

2025-02-10 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy edited 
https://github.com/llvm/llvm-project/pull/125937
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Appropriately set function attribute optnone (PR #125937)

2025-02-10 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy edited 
https://github.com/llvm/llvm-project/pull/125937
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Appropriately set function attribute optnone (PR #125937)

2025-02-10 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy edited 
https://github.com/llvm/llvm-project/pull/125937
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Appropriately set function attribute optnone (PR #125937)

2025-02-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy edited 
https://github.com/llvm/llvm-project/pull/125937
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Appropriately set function attribute optnone (PR #125937)

2025-02-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy edited 
https://github.com/llvm/llvm-project/pull/125937
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Appropriately set function attribute optnone (PR #125937)

2025-02-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy updated 
https://github.com/llvm/llvm-project/pull/125937

>From 63a728dd7f5efff32a2f90608adfdc13d540f34f Mon Sep 17 00:00:00 2001
From: "S. Bharadwaj Yadavalli" 
Date: Tue, 4 Feb 2025 12:48:09 -0500
Subject: [PATCH] [HLSL] Set function optnone attribute appropriately

When optimization is disabled, set optnone attribute
  - for all module functions when targetting Library shaders
  - only for entry function when targetting non-Library shaders

Update tests in accordance with the change.
---
 clang/lib/CodeGen/CGHLSLRuntime.cpp   |  10 +
 .../CodeGenHLSL/GlobalConstructorLib.hlsl |   8 +-
 clang/test/CodeGenHLSL/GlobalDestructors.hlsl |   4 +-
 .../test/CodeGenHLSL/inline-constructors.hlsl |  16 +-
 clang/test/CodeGenHLSL/inline-functions.hlsl  | 189 +-
 .../CodeGenHLSL/this-assignment-overload.hlsl |   6 +-
 6 files changed, 174 insertions(+), 59 deletions(-)

diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 2ce54cc3c52efa0..24acc9a559be2df 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -345,6 +345,9 @@ void clang::CodeGen::CGHLSLRuntime::setHLSLEntryAttributes(
 WaveSizeAttr->getPreferred());
 Fn->addFnAttr(WaveSizeKindStr, WaveSizeStr);
   }
+  if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
+Fn->addFnAttr(llvm::Attribute::OptimizeNone);
+  }
   Fn->addFnAttr(llvm::Attribute::NoInline);
 }
 
@@ -446,6 +449,13 @@ void CGHLSLRuntime::setHLSLFunctionAttributes(const 
FunctionDecl *FD,
 const StringRef ExportAttrKindStr = "hlsl.export";
 Fn->addFnAttr(ExportAttrKindStr);
   }
+  llvm::Triple T(Fn->getParent()->getTargetTriple());
+  if (T.getEnvironment() == llvm::Triple::EnvironmentType::Library) {
+if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
+  Fn->addFnAttr(llvm::Attribute::OptimizeNone);
+  Fn->addFnAttr(llvm::Attribute::NoInline);
+}
+  }
 }
 
 static void gatherFunctions(SmallVectorImpl &Fns, llvm::Module &M,
diff --git a/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl 
b/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl
index 09c44f6242c53c7..39d7c73e832a104 100644
--- a/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl
+++ b/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl
@@ -1,5 +1,5 @@
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm 
-disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=CHECK,NOINLINE
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -O0 %s -o 
- | FileCheck %s --check-prefixes=CHECK,INLINE
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -O1 %s -o 
- | FileCheck %s --check-prefixes=CHECK,INLINE
 
 // Make sure global variable for ctors exist for lib profile.
 // CHECK:@llvm.global_ctors
@@ -31,12 +31,12 @@ void SecondEntry() {}
 // CHECK: ret void
 
 
-// Verify the constructor is alwaysinline
-// NOINLINE: ; Function Attrs: {{.*}}alwaysinline
+// Verify the constructor is optnone
+// NOINLINE: ; Function Attrs: {{.*}} optnone
 // NOINLINE-NEXT: define linkonce_odr void @_ZN4hlsl8RWBufferIfEC2Ev({{.*}} 
[[CtorAttr:\#[0-9]+]]
 
 // NOINLINE: ; Function Attrs: {{.*}}alwaysinline
 // NOINLINE-NEXT: define internal void 
@_GLOBAL__sub_I_GlobalConstructorLib.hlsl() [[InitAttr:\#[0-9]+]]
 
 // NOINLINE-DAG: attributes [[InitAttr]] = {{.*}} alwaysinline
-// NOINLINE-DAG: attributes [[CtorAttr]] = {{.*}} alwaysinline
+// NOINLINE-DAG: attributes [[CtorAttr]] = {{.*}} optnone
diff --git a/clang/test/CodeGenHLSL/GlobalDestructors.hlsl 
b/clang/test/CodeGenHLSL/GlobalDestructors.hlsl
index f98318601134bb2..8961e1a7e59cd9c 100644
--- a/clang/test/CodeGenHLSL/GlobalDestructors.hlsl
+++ b/clang/test/CodeGenHLSL/GlobalDestructors.hlsl
@@ -1,7 +1,7 @@
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -emit-llvm 
-disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=CS,NOINLINE,CHECK
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm 
-disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=LIB,NOINLINE,CHECK
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -emit-llvm -O0 %s -o 
- | FileCheck %s --check-prefixes=INLINE,CHECK
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -O0 %s -o 
- | FileCheck %s --check-prefixes=INLINE,CHECK
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -emit-llvm -O1 %s -o 
- | FileCheck %s --check-prefixes=INLINE,CHECK
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -O1 %s -o 
- | FileCheck %s --check-prefixes=INLINE,CHECK
 
 // Tests that constructors and destructors are appropriately generated for 
globals
 // and that their calls are inlined when AlwaysInline is run
diff --git a/clang/test/CodeGenHLSL/inline-constructors.hlsl 
b/clang/test/CodeGenHLSL/inline-constructors.hlsl
index b0d5a783fb3725c..298d7d42726789c 100644
--- a/clang/test/CodeGenHLSL/inline-constructors.hlsl
+++ b/clang/test/Co

[clang] [HLSL] Appropriately set function attribute optnone (PR #125937)

2025-02-06 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy ready_for_review 
https://github.com/llvm/llvm-project/pull/125937
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Appropriately set function attribute optnone (PR #125937)

2025-02-06 Thread S. Bharadwaj Yadavalli via cfe-commits

bharadwajy wrote:

> Need to verify a couple more function attribute combinations. Changed it to 
> draft mode till that is done.
> 
> Please hold off reviews.

PR ready for review. Thanks!

https://github.com/llvm/llvm-project/pull/125937
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Appropriately set function attribute optnone (PR #125937)

2025-02-05 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy created 
https://github.com/llvm/llvm-project/pull/125937

When optimization is disabled, set optnone attribute
  - for all module functions when targetting library shaders
  - only for entry function when targetting non-library shaders

Updated tests in accordance with the change.

With this change, generation of non-standard DXIL metadata 
`dx.disable_optimizations` is planned to be eliminated (#124796) in a follow-on 
PR. 

>From f04a746ae9f11c3aa73e22690ad46ba6c7df133d Mon Sep 17 00:00:00 2001
From: "S. Bharadwaj Yadavalli" 
Date: Tue, 4 Feb 2025 12:48:09 -0500
Subject: [PATCH] [HLSL] Set function optnone attribute appropriately

When optimization is disabled, set optnone attribute
  - for all module functions when targetting Library shaders
  - only for entry function when targetting non-Library shaders

Update tests in accordance with the change.
---
 clang/lib/CodeGen/CodeGenModule.cpp   |  11 +-
 .../GlobalConstructorFunction.hlsl|   5 +-
 .../CodeGenHLSL/GlobalConstructorLib.hlsl |   8 +-
 clang/test/CodeGenHLSL/GlobalDestructors.hlsl |   4 +-
 .../builtins/RWBuffer-subscript.hlsl  |   3 +-
 .../StructuredBuffers-subscripts.hlsl |   3 +-
 .../implicit-norecurse-attrib.hlsl|  31 ++--
 .../test/CodeGenHLSL/inline-constructors.hlsl |  14 +-
 clang/test/CodeGenHLSL/inline-functions.hlsl  | 158 +-
 .../CodeGenHLSL/this-assignment-overload.hlsl |   6 +-
 10 files changed, 164 insertions(+), 79 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp 
b/clang/lib/CodeGen/CodeGenModule.cpp
index 02615bb13dfb8a7..42cb7d8634a24b2 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -2545,7 +2545,16 @@ void 
CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
   // Non-entry HLSL functions must always be inlined.
   if (getLangOpts().HLSL && !F->hasFnAttribute(llvm::Attribute::NoInline) &&
   !D->hasAttr()) {
-B.addAttribute(llvm::Attribute::AlwaysInline);
+// Set OptimizeNone for HLSL entry functions if ShouldAddOptNone
+// or for all HLSL functions compiled for Library target.
+llvm::Triple T(F->getParent()->getTargetTriple());
+if (ShouldAddOptNone &&
+(D->hasAttr() ||
+ T.getEnvironment() == llvm::Triple::EnvironmentType::Library)) {
+  B.addAttribute(llvm::Attribute::OptimizeNone);
+  B.addAttribute(llvm::Attribute::NoInline);
+} else
+  B.addAttribute(llvm::Attribute::AlwaysInline);
   } else if ((ShouldAddOptNone || D->hasAttr()) &&
  !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
 // Add optnone, but do so only if the function isn't always_inline.
diff --git a/clang/test/CodeGenHLSL/GlobalConstructorFunction.hlsl 
b/clang/test/CodeGenHLSL/GlobalConstructorFunction.hlsl
index c0eb1b138ed0475..e880ec93c634ed5 100644
--- a/clang/test/CodeGenHLSL/GlobalConstructorFunction.hlsl
+++ b/clang/test/CodeGenHLSL/GlobalConstructorFunction.hlsl
@@ -32,11 +32,10 @@ void main(unsigned GI : SV_GroupIndex) {}
 // NOINLINE-NEXT:   call void @_Z12call_me_lastv(
 // NOINLINE-NEXT:   ret void
 
-// Verify constructor calls are inlined when AlwaysInline is run
-// INLINE-NEXT:   alloca
+// Verify constructor calls are inlined
 // INLINE-NEXT:   store i32 12
 // INLINE-NEXT:   store i32 13
 // INLINE-NEXT:   %0 = call i32 @llvm.dx.flattened.thread.id.in.group()
-// INLINE-NEXT:   store i32 %
+// INLINE-NEXT:   call void @_Z4mainj(i32 %0)
 // INLINE-NEXT:   store i32 0
 // INLINE:   ret void
diff --git a/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl 
b/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl
index 09c44f6242c53c7..39d7c73e832a104 100644
--- a/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl
+++ b/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl
@@ -1,5 +1,5 @@
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm 
-disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=CHECK,NOINLINE
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -O0 %s -o 
- | FileCheck %s --check-prefixes=CHECK,INLINE
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -O1 %s -o 
- | FileCheck %s --check-prefixes=CHECK,INLINE
 
 // Make sure global variable for ctors exist for lib profile.
 // CHECK:@llvm.global_ctors
@@ -31,12 +31,12 @@ void SecondEntry() {}
 // CHECK: ret void
 
 
-// Verify the constructor is alwaysinline
-// NOINLINE: ; Function Attrs: {{.*}}alwaysinline
+// Verify the constructor is optnone
+// NOINLINE: ; Function Attrs: {{.*}} optnone
 // NOINLINE-NEXT: define linkonce_odr void @_ZN4hlsl8RWBufferIfEC2Ev({{.*}} 
[[CtorAttr:\#[0-9]+]]
 
 // NOINLINE: ; Function Attrs: {{.*}}alwaysinline
 // NOINLINE-NEXT: define internal void 
@_GLOBAL__sub_I_GlobalConstructorLib.hlsl() [[InitAttr:\#[0-9]+]]
 
 // NOINLINE-DAG: attributes [[InitAttr]] = {{.*}} alwaysinline
-// NOINLINE-DAG: attributes [[CtorAttr]] = {{.*}} alwaysinline
+// N

[clang] [HLSL] Appropriately set function attribute optnone (PR #125937)

2025-02-05 Thread S. Bharadwaj Yadavalli via cfe-commits

bharadwajy wrote:

Need to verify a couple more function attribute combinations. Changed it to 
draft mode till that is done. 

Please hold off reviews.

https://github.com/llvm/llvm-project/pull/125937
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Appropriately set function attribute optnone (PR #125937)

2025-02-05 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy converted_to_draft 
https://github.com/llvm/llvm-project/pull/125937
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [HLSL] Appropriately set function attribute optnone (PR #125937)

2025-02-11 Thread S. Bharadwaj Yadavalli via cfe-commits

https://github.com/bharadwajy updated 
https://github.com/llvm/llvm-project/pull/125937

>From 63a728dd7f5efff32a2f90608adfdc13d540f34f Mon Sep 17 00:00:00 2001
From: "S. Bharadwaj Yadavalli" 
Date: Tue, 4 Feb 2025 12:48:09 -0500
Subject: [PATCH 1/4] [HLSL] Set function optnone attribute appropriately

When optimization is disabled, set optnone attribute
  - for all module functions when targetting Library shaders
  - only for entry function when targetting non-Library shaders

Update tests in accordance with the change.
---
 clang/lib/CodeGen/CGHLSLRuntime.cpp   |  10 +
 .../CodeGenHLSL/GlobalConstructorLib.hlsl |   8 +-
 clang/test/CodeGenHLSL/GlobalDestructors.hlsl |   4 +-
 .../test/CodeGenHLSL/inline-constructors.hlsl |  16 +-
 clang/test/CodeGenHLSL/inline-functions.hlsl  | 189 +-
 .../CodeGenHLSL/this-assignment-overload.hlsl |   6 +-
 6 files changed, 174 insertions(+), 59 deletions(-)

diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp 
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index 2ce54cc3c52ef..24acc9a559be2 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -345,6 +345,9 @@ void clang::CodeGen::CGHLSLRuntime::setHLSLEntryAttributes(
 WaveSizeAttr->getPreferred());
 Fn->addFnAttr(WaveSizeKindStr, WaveSizeStr);
   }
+  if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
+Fn->addFnAttr(llvm::Attribute::OptimizeNone);
+  }
   Fn->addFnAttr(llvm::Attribute::NoInline);
 }
 
@@ -446,6 +449,13 @@ void CGHLSLRuntime::setHLSLFunctionAttributes(const 
FunctionDecl *FD,
 const StringRef ExportAttrKindStr = "hlsl.export";
 Fn->addFnAttr(ExportAttrKindStr);
   }
+  llvm::Triple T(Fn->getParent()->getTargetTriple());
+  if (T.getEnvironment() == llvm::Triple::EnvironmentType::Library) {
+if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
+  Fn->addFnAttr(llvm::Attribute::OptimizeNone);
+  Fn->addFnAttr(llvm::Attribute::NoInline);
+}
+  }
 }
 
 static void gatherFunctions(SmallVectorImpl &Fns, llvm::Module &M,
diff --git a/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl 
b/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl
index 09c44f6242c53..39d7c73e832a1 100644
--- a/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl
+++ b/clang/test/CodeGenHLSL/GlobalConstructorLib.hlsl
@@ -1,5 +1,5 @@
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm 
-disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=CHECK,NOINLINE
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -O0 %s -o 
- | FileCheck %s --check-prefixes=CHECK,INLINE
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -O1 %s -o 
- | FileCheck %s --check-prefixes=CHECK,INLINE
 
 // Make sure global variable for ctors exist for lib profile.
 // CHECK:@llvm.global_ctors
@@ -31,12 +31,12 @@ void SecondEntry() {}
 // CHECK: ret void
 
 
-// Verify the constructor is alwaysinline
-// NOINLINE: ; Function Attrs: {{.*}}alwaysinline
+// Verify the constructor is optnone
+// NOINLINE: ; Function Attrs: {{.*}} optnone
 // NOINLINE-NEXT: define linkonce_odr void @_ZN4hlsl8RWBufferIfEC2Ev({{.*}} 
[[CtorAttr:\#[0-9]+]]
 
 // NOINLINE: ; Function Attrs: {{.*}}alwaysinline
 // NOINLINE-NEXT: define internal void 
@_GLOBAL__sub_I_GlobalConstructorLib.hlsl() [[InitAttr:\#[0-9]+]]
 
 // NOINLINE-DAG: attributes [[InitAttr]] = {{.*}} alwaysinline
-// NOINLINE-DAG: attributes [[CtorAttr]] = {{.*}} alwaysinline
+// NOINLINE-DAG: attributes [[CtorAttr]] = {{.*}} optnone
diff --git a/clang/test/CodeGenHLSL/GlobalDestructors.hlsl 
b/clang/test/CodeGenHLSL/GlobalDestructors.hlsl
index f98318601134b..8961e1a7e59cd 100644
--- a/clang/test/CodeGenHLSL/GlobalDestructors.hlsl
+++ b/clang/test/CodeGenHLSL/GlobalDestructors.hlsl
@@ -1,7 +1,7 @@
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -emit-llvm 
-disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=CS,NOINLINE,CHECK
 // RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm 
-disable-llvm-passes %s -o - | FileCheck %s --check-prefixes=LIB,NOINLINE,CHECK
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -emit-llvm -O0 %s -o 
- | FileCheck %s --check-prefixes=INLINE,CHECK
-// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -O0 %s -o 
- | FileCheck %s --check-prefixes=INLINE,CHECK
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -emit-llvm -O1 %s -o 
- | FileCheck %s --check-prefixes=INLINE,CHECK
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.3-library -emit-llvm -O1 %s -o 
- | FileCheck %s --check-prefixes=INLINE,CHECK
 
 // Tests that constructors and destructors are appropriately generated for 
globals
 // and that their calls are inlined when AlwaysInline is run
diff --git a/clang/test/CodeGenHLSL/inline-constructors.hlsl 
b/clang/test/CodeGenHLSL/inline-constructors.hlsl
index b0d5a783fb372..298d7d4272678 100644
--- a/clang/test/CodeGenHLSL/inline-constructors.hlsl
+++ b/clang/test/CodeGenHLSL/in

  1   2   >