[clang] [HLSL] Allow narrowing in initialization lists (PR #108035)

2024-09-12 Thread Cooper Partin via cfe-commits

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

looks fine.  Curious, why is this extra RUN: line added?  Doesn't the other one 
already contain the same parameters `-ast-dump -o - %s | FileCheck %s` ?

RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -ast-dump -o - %s | 
FileCheck %s

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


[clang] [NFC] [HLSL] Update test for HLSL 202x (PR #108097)

2024-09-10 Thread Cooper Partin via cfe-commits

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

Looks fine to me.  Am I correct that there will be quite a few more incoming 
changes for the other tests that use hlsl2021 or older settings?

https://github.com/llvm/llvm-project/pull/108097
___
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 Cooper Partin via cfe-commits

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

This looks good to me.

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] [HLSL] Implement support for HLSL intrinsic - select (PR #107129)

2024-09-03 Thread Cooper Partin via cfe-commits


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

coopp wrote:

Sounds good.

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] [HLSL] Implement support for HLSL intrinsic - select (PR #107129)

2024-09-03 Thread Cooper Partin via cfe-commits


@@ -18695,6 +18695,50 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
 CGM.getHLSLRuntime().getSaturateIntrinsic(), ArrayRef{Op0},
 nullptr, "hlsl.saturate");
   }
+  case Builtin::BI__builtin_hlsl_select: {
+Value *OpCond = EmitScalarExpr(E->getArg(0));
+Value *OpTrue = EmitScalarExpr(E->getArg(1));
+Value *OpFalse = EmitScalarExpr(E->getArg(2));
+llvm::Type *TCond = OpCond->getType();
+
+// if cond is a bool emit a select instruction
+if (TCond->isIntegerTy(1))
+  return Builder.CreateSelect(OpCond, OpTrue, OpFalse, "hlsl.select");
+
+// if cond is a vector of bools lower to a shufflevector
+// todo check if that true and false are vectors

coopp wrote:

Very cool.. I do the same.

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] [HLSL] Implement support for HLSL intrinsic - select (PR #107129)

2024-09-03 Thread Cooper Partin via cfe-commits


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

coopp wrote:

Did you mean for 'Builtin::BI__builtin_hlsl_elementwise_saturate' to fall 
through into 'Builtin::BI__builtin_hlsl_select' logic in this switch( ) 
statement?

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] [HLSL] Implement support for HLSL intrinsic - select (PR #107129)

2024-09-03 Thread Cooper Partin via cfe-commits


@@ -18695,6 +18695,50 @@ case Builtin::BI__builtin_hlsl_elementwise_isinf: {
 CGM.getHLSLRuntime().getSaturateIntrinsic(), ArrayRef{Op0},
 nullptr, "hlsl.saturate");
   }
+  case Builtin::BI__builtin_hlsl_select: {
+Value *OpCond = EmitScalarExpr(E->getArg(0));
+Value *OpTrue = EmitScalarExpr(E->getArg(1));
+Value *OpFalse = EmitScalarExpr(E->getArg(2));
+llvm::Type *TCond = OpCond->getType();
+
+// if cond is a bool emit a select instruction
+if (TCond->isIntegerTy(1))
+  return Builder.CreateSelect(OpCond, OpTrue, OpFalse, "hlsl.select");
+
+// if cond is a vector of bools lower to a shufflevector
+// todo check if that true and false are vectors

coopp wrote:

There are some 'todo' comments here.  Are these just for your own bookkeeping 
while working on this?  Or are they stil something you need to do?  The code 
below looks like you addressed them.  I could be missing something.

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] [HLSL] update default validator version to 1.8. (PR #104040)

2024-08-15 Thread Cooper Partin via cfe-commits

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

Looks good.

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


[clang] [HLSL] update default validator version to 1.8. (PR #104040)

2024-08-14 Thread Cooper Partin via cfe-commits


@@ -283,14 +283,6 @@ HLSLToolChain::TranslateArgs(const DerivedArgList &Args, 
StringRef BoundArch,
 DAL->append(A);
   }
 
-  // Add default validator version if not set.

coopp wrote:

This was removed.  Do we still need to fallback to a default here or does the 
change in Options.td take care of all of that?

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


[clang] [HLSL] Implement output parameter (PR #101083)

2024-08-07 Thread Cooper Partin via cfe-commits

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

That was a lot!.  Didn't see anything to call out, so it looks good to me.

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


[clang] [llvm] [SPIRV][HLSL] Add lowering of frac to SPIR-V (PR #97111)

2024-07-23 Thread Cooper Partin via cfe-commits

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

Looks good to me.

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


[clang] [Doc][HLSL] Add documentation for root signature (PR #88781)

2024-07-17 Thread Cooper Partin via cfe-commits


@@ -0,0 +1,292 @@
+
+HLSL Root Signatures
+
+
+.. contents::
+   :local:
+
+Usage
+=
+
+In HLSL, the `root signature
+`_
+defines what types of resources are bound to the graphics pipeline.
+
+A root signature can be specified in HLSL as a `string
+`_.
+The string contains a collection of comma-separated clauses that describe root
+signature constituent components.
+
+There are two mechanisms to compile an HLSL root signature. First, it is
+possible to attach a root signature string to a particular shader via the
+RootSignature attribute (in the following example, using the main entry
+point):
+
+.. code-block::
+
+#define RS "RootFlags( ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT | " \
+  "DENY_VERTEX_SHADER_ROOT_ACCESS), " \
+  "CBV(b0, space = 1, flags = DATA_STATIC), " \
+  "SRV(t0), " \
+  "UAV(u0), " \
+  "DescriptorTable( CBV(b1), " \
+  " SRV(t1, numDescriptors = 8, " \
+  " flags = DESCRIPTORS_VOLATILE), " \
+  " UAV(u1, numDescriptors = unbounded, " \
+  " flags = DESCRIPTORS_VOLATILE)), " \
+  "DescriptorTable(Sampler(s0, space=1, numDescriptors = 4)), " \
+  "RootConstants(num32BitConstants=3, b10), " \
+  "StaticSampler(s1)," \
+  "StaticSampler(s2, " \
+  "  addressU = TEXTURE_ADDRESS_CLAMP, " \
+  "  filter = FILTER_MIN_MAG_MIP_LINEAR )"
+
+[RootSignature(MyRS)]
+float4 main(float4 coord : COORD) : SV_Target
+{
+…
+}
+
+The compiler will create and verify the root signature blob for the shader and
+embed it alongside the shader byte code into the shader blob.
+
+The other mechanism is to create a standalone root signature blob, perhaps to
+reuse it with a large set of shaders, saving space. The name of the define
+string is specified via the usual -E argument. For example:

coopp wrote:

I am confused.  I thought the rootsignature define was specified on the 
commandline as -rootsig-define ?  And -E was to specify the shader entry point.

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


[clang] [Doc][HLSL] Add documentation for root signature (PR #88781)

2024-07-17 Thread Cooper Partin via cfe-commits


@@ -0,0 +1,292 @@
+
+HLSL Root Signatures
+
+
+.. contents::
+   :local:
+
+Usage
+=
+
+In HLSL, the `root signature
+`_
+defines what types of resources are bound to the graphics pipeline.
+
+A root signature can be specified in HLSL as a `string
+`_.
+The string contains a collection of comma-separated clauses that describe root
+signature constituent components.
+
+There are two mechanisms to compile an HLSL root signature. First, it is
+possible to attach a root signature string to a particular shader via the
+RootSignature attribute (in the following example, using the main entry
+point):
+
+.. code-block::
+
+#define RS "RootFlags( ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT | " \
+  "DENY_VERTEX_SHADER_ROOT_ACCESS), " \
+  "CBV(b0, space = 1, flags = DATA_STATIC), " \
+  "SRV(t0), " \
+  "UAV(u0), " \
+  "DescriptorTable( CBV(b1), " \
+  " SRV(t1, numDescriptors = 8, " \
+  " flags = DESCRIPTORS_VOLATILE), " \
+  " UAV(u1, numDescriptors = unbounded, " \
+  " flags = DESCRIPTORS_VOLATILE)), " \
+  "DescriptorTable(Sampler(s0, space=1, numDescriptors = 4)), " \
+  "RootConstants(num32BitConstants=3, b10), " \
+  "StaticSampler(s1)," \
+  "StaticSampler(s2, " \
+  "  addressU = TEXTURE_ADDRESS_CLAMP, " \
+  "  filter = FILTER_MIN_MAG_MIP_LINEAR )"
+
+[RootSignature(MyRS)]

coopp wrote:

In this sample the Root signature is #define RS, but the [RootSignature( ) ] 
statement is referring to 'MyRS'.. Should this be 'RS' instead?

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


[clang] [HLSL] Use llvm::Triple::EnvironmentType instead of HLSLShaderAttr::ShaderType (PR #93847)

2024-05-30 Thread Cooper Partin via cfe-commits

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

Looks great!  I like how clean this change is!

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


[clang] [llvm] [clang] Add tanf16 builtin and support for tan constrained intrinsic (PR #93314)

2024-05-28 Thread Cooper Partin via cfe-commits

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

Looks good to me.

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


[clang] [HLSL][CMake] Cache files don't have generator vars (PR #92793)

2024-05-20 Thread Cooper Partin via cfe-commits

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

Looks good to me.

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


[clang] HLSL availability diagnostics design doc (PR #92207)

2024-05-15 Thread Cooper Partin via cfe-commits

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

This looks good to me.

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


[clang] HLSL availability diagnostics design doc (PR #92207)

2024-05-15 Thread Cooper Partin via cfe-commits


@@ -0,0 +1,53 @@
+=
+HLSL Availability Diagnostics
+=
+
+.. contents::
+   :local:
+
+Introduction
+
+
+HLSL availability diagnostics emits errors or warning when unavailable shader 
APIs are used. Unavailable shader APIs are APIs that are exposed in HLSL code 
but are not available in the target shader stage or shader model version.
+
+There are three modes of HLSL availability diagnostic:
+
+#. **Default mode** - compiler emits an error when an unavailable shader API 
is found in a code that is reachable from the shader entry point function or 
from an exported library function (when compiling a shader library)
+
+#. **Relaxed mode** - same as default mode except the compiler emits a 
warning. This mode is enabled by ``-Wno-error=hlsl-availability``.
+
+#. **Strict mode** - compiler emits an error when when an unavailable API is 
found in parsed code regardless of whether it can be reached from the shader 
entry point or exported functions, or not. This mode is enabled by 
``-fhlsl-strict-diagnostics``.
+
+Implementation Details
+==
+
+Environment Parameter
+-
+
+In order to encode API availability based on the shader model version and 
shader model stage a new ``environment`` parameter was added to the existing 
Clang ``availability`` attribute. 
+
+The values allowed for this parameter are a subset of values allowed as the 
``llvm::Triple`` environment component. If the environment parameters is 
present, the declared availability attribute applies only to targets with the 
same platform and environment.
+
+Default and Relaxed Diagnostic Modes
+
+
+This mode is implemeted in ``DiagnoseHLSLAvailability`` class in 
``SemaHLSL.cpp`` and it is invoked after the whole translation unit is parsed 
(from ``Sema::ActOnEndOfTranslationUnit``). The implementation iterates over 
all shader entry points and exported library functions in the translation unit 
and performs an AST traversal of each function body.
+
+When a reference to another function is found and it has a body, the AST of 
the referenced function is also scanned. This chain of AST traversals will 
reach all of the code that is reachable from the initial shader entry point or 
exported library function.
+
+All shader APIs have an availability attribute that specifies the shader model 
version (and environment, if applicable) when this API was first 
introduced.When a reference to a function without a definition is found and it 
has an availability attribute, the version of the attribute is checked against 
the target shader model version and shader stage (if shader stage context is 
known), and an appropriate diagnostic is generated as needed.
+
+All shader entry functions have ``HLSLShaderAttr`` attribute that specifies 
what type of shader this function represents. However, for exported library 
functions the target shader stage is unknown, so in this case the HLSL API 
availability will be only checked against the shader model version.
+
+A list of functions that were already scanned is kept in order to avoid 
duplicate scans and diagnostics (see 
``DiagnoseHLSLAvailability::ScannedDecls``). It might happen that a shader 
library has multiple shader entry points for different shader stages that all 
call into the same shared function. It is therefore important to record not 
just that a function has been scanned, but also in which shader stage context. 
This is done by using ``llvm::DenseMap`` that maps ``FunctionDecl *`` to a 
``unsigned`` bitmap that represents a set of shader stages (or environments) 
the function has been scanned for. The ``N``'th bit in the set is set if the 
function has been scanned in shader environment whose 
``HLSLShaderAttr::ShaderType`` integer value equals ``N``.
+
+The emitted diagnostic messages belong to ``hlsl-availability`` diagnostic 
group and are reported as errors by default. With 
``-Wno-error=hlsl-availability`` flang they become warning, making it relaxed 
HLSL diagnostics mode.

coopp wrote:

nit: flang --> flag?

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


[clang] HLSL availability diagnostics design doc (PR #92207)

2024-05-15 Thread Cooper Partin via cfe-commits


@@ -0,0 +1,53 @@
+=
+HLSL Availability Diagnostics
+=
+
+.. contents::
+   :local:
+
+Introduction
+
+
+HLSL availability diagnostics emits errors or warning when unavailable shader 
APIs are used. Unavailable shader APIs are APIs that are exposed in HLSL code 
but are not available in the target shader stage or shader model version.
+
+There are three modes of HLSL availability diagnostic:
+
+#. **Default mode** - compiler emits an error when an unavailable shader API 
is found in a code that is reachable from the shader entry point function or 
from an exported library function (when compiling a shader library)
+
+#. **Relaxed mode** - same as default mode except the compiler emits a 
warning. This mode is enabled by ``-Wno-error=hlsl-availability``.
+
+#. **Strict mode** - compiler emits an error when when an unavailable API is 
found in parsed code regardless of whether it can be reached from the shader 
entry point or exported functions, or not. This mode is enabled by 
``-fhlsl-strict-diagnostics``.
+
+Implementation Details
+==
+
+Environment Parameter
+-
+
+In order to encode API availability based on the shader model version and 
shader model stage a new ``environment`` parameter was added to the existing 
Clang ``availability`` attribute. 
+
+The values allowed for this parameter are a subset of values allowed as the 
``llvm::Triple`` environment component. If the environment parameters is 
present, the declared availability attribute applies only to targets with the 
same platform and environment.
+
+Default and Relaxed Diagnostic Modes
+
+
+This mode is implemeted in ``DiagnoseHLSLAvailability`` class in 
``SemaHLSL.cpp`` and it is invoked after the whole translation unit is parsed 
(from ``Sema::ActOnEndOfTranslationUnit``). The implementation iterates over 
all shader entry points and exported library functions in the translation unit 
and performs an AST traversal of each function body.

coopp wrote:

nit: implemented

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


[clang] HLSL availability diagnostics design doc (PR #92207)

2024-05-15 Thread Cooper Partin via cfe-commits


@@ -0,0 +1,53 @@
+=
+HLSL Availability Diagnostics
+=
+
+.. contents::
+   :local:
+
+Introduction
+
+
+HLSL availability diagnostics emits errors or warning when unavailable shader 
APIs are used. Unavailable shader APIs are APIs that are exposed in HLSL code 
but are not available in the target shader stage or shader model version.
+
+There are three modes of HLSL availability diagnostic:
+
+#. **Default mode** - compiler emits an error when an unavailable shader API 
is found in a code that is reachable from the shader entry point function or 
from an exported library function (when compiling a shader library)

coopp wrote:

I like how you phrased the emitting an error statement in your Strict mode 
description.  This description refers to the API as 'unavailable shader API'.  
I think unavailable API reads better as all APIs are in the shader anyway.

Stolen from Strict mode:  "compiler emits an error when an unavailable API is 
found in parsed code ..."

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


[clang] HLSL availability diagnostics design doc (PR #92207)

2024-05-15 Thread Cooper Partin via cfe-commits


@@ -0,0 +1,53 @@
+=
+HLSL Availability Diagnostics
+=
+
+.. contents::
+   :local:
+
+Introduction
+
+
+HLSL availability diagnostics emits errors or warning when unavailable shader 
APIs are used. Unavailable shader APIs are APIs that are exposed in HLSL code 
but are not available in the target shader stage or shader model version.
+
+There are three modes of HLSL availability diagnostic:
+
+#. **Default mode** - compiler emits an error when an unavailable shader API 
is found in a code that is reachable from the shader entry point function or 
from an exported library function (when compiling a shader library)
+
+#. **Relaxed mode** - same as default mode except the compiler emits a 
warning. This mode is enabled by ``-Wno-error=hlsl-availability``.
+
+#. **Strict mode** - compiler emits an error when when an unavailable API is 
found in parsed code regardless of whether it can be reached from the shader 
entry point or exported functions, or not. This mode is enabled by 
``-fhlsl-strict-diagnostics``.

coopp wrote:

Extra "when" in Strict mode description.

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


[clang] Enable unguarded availability diagnostic on instantiated template functions (PR #91699)

2024-05-10 Thread Cooper Partin via cfe-commits

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

Looks fine to me.  I assume this doesn't impact other test passes right?  Now 
there is work being done when before it was doing an early out for template 
instantiations.

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


[clang] Enable unguarded availability diagnostic on instantiated template functions (PR #91699)

2024-05-10 Thread Cooper Partin via cfe-commits

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


[clang] Enable unguarded availability diagnostic on instantiated template functions (PR #91699)

2024-05-10 Thread Cooper Partin via cfe-commits


@@ -177,16 +177,19 @@ void justAtAvailable(void) {
 
 #ifdef OBJCPP
 
-int f(char) AVAILABLE_10_12;
+int f(char) AVAILABLE_10_12; // #f_char_def
 int f(int);
 
 template  int use_f() {
-  // FIXME: We should warn here!
-  return f(T());
+  // expected-warning@#f_call {{'f' is only available on macOS 10.12 or newer}}
+  // expected-note@#f_char_inst {{in instantiation of function template 
specialization 'use_f' requested here}}
+  // expected-note@#f_char_def {{'f' has been marked as being introduced in 
macOS 10.12 here, but the deployment target is macOS 10.9}}
+  // expected-note@#f_call {{enclose 'f' in an @available check to silence 
this warning}}
+  return f(T()); // #f_call
 }
 
 int a = use_f();
-int b = use_f();
+int b = use_f(); // #f_char_inst

coopp wrote:

I don't see other "// $f_foo_inst" comments in other parts of the code.  Why 
was this one special to get one?  Was it just for tracking the change?

https://github.com/llvm/llvm-project/pull/91699
___
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 Cooper Partin via cfe-commits

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

Looks good.  Took me a while to go through all of the tests.  Looks like 
everything is covered!

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] [NFC][HLSL] Cleanup TargetInfo handling (PR #90694)

2024-05-01 Thread Cooper Partin via cfe-commits

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

Just a small nit from me about switch( ) statement formatting in existing code. 
 Not the new code.  Looks good to me.

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


[clang] [NFC][HLSL] Cleanup TargetInfo handling (PR #90694)

2024-05-01 Thread Cooper Partin via cfe-commits


@@ -1612,15 +1612,7 @@ const llvm::fltSemantics 
&ASTContext::getFloatTypeSemantics(QualType T) const {
   case BuiltinType::Float16:
 return Target->getHalfFormat();
   case BuiltinType::Half:
-// For HLSL, when the native half type is disabled, half will be treat as
-// float.
-if (getLangOpts().HLSL)
-  if (getLangOpts().NativeHalfType)
-return Target->getHalfFormat();
-  else
-return Target->getFloatFormat();
-else
-  return Target->getHalfFormat();
+return Target->getHalfFormat();
   case BuiltinType::Float:  return Target->getFloatFormat();

coopp wrote:

Existing (not in your new changes):  This switch statement is strangely 
formatted.  I seem some cases with the return on the same line as the case: and 
others on the next line.  Maybe this is how clang-format handles this?

Example:

case BuiltinType::Double: return Target->getDoubleFormat();
  case BuiltinType::Ibm128:
return Target->getIbm128Format();



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


[clang] [NFC] Rename hlsl semantics to hlsl annotations (PR #89309)

2024-04-18 Thread Cooper Partin via cfe-commits

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

Looks good

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


[clang] [NFC] Rename hlsl semantics to hlsl annotations (PR #89309)

2024-04-18 Thread Cooper Partin via cfe-commits


@@ -4890,15 +4890,16 @@ class SpellingList {
   }
 
   void add(const Record &Attr, FlattenedSpelling Spelling) {
-SpellingKind Kind = StringSwitch(Spelling.variety())
-.Case("GNU", SpellingKind::GNU)
-.Case("CXX11", SpellingKind::CXX11)
-.Case("C23", SpellingKind::C23)
-.Case("Declspec", SpellingKind::Declspec)
-.Case("Microsoft", SpellingKind::Microsoft)
-.Case("Keyword", SpellingKind::Keyword)
-.Case("Pragma", SpellingKind::Pragma)
-.Case("HLSLSemantic", SpellingKind::HLSLSemantic);
+SpellingKind Kind =

coopp wrote:

Did clang-format shift all of this code over?

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


[clang] [clang][CodeGen] Fix shift-exponent ubsan check for signed _BitInt (PR #88004)

2024-04-18 Thread Cooper Partin via cfe-commits

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

Looks good to me.

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


[clang] [clang][CodeGen] Fix shift-exponent ubsan check for signed _BitInt (PR #88004)

2024-04-18 Thread Cooper Partin via cfe-commits


@@ -146,6 +146,15 @@ struct BinOpInfo {
   return UnOp->getSubExpr()->getType()->isFixedPointType();
 return false;
   }
+
+  /// Check if the RHS has a signed integer representation.

coopp wrote:

Extra '/' in comment line.

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


[clang] [clang][SPIR-V] Set AS for the SPIR-V logical triple (PR #88939)

2024-04-16 Thread Cooper Partin via cfe-commits

https://github.com/coopp commented:

Looks good

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


[clang] [llvm] [SPIRV][HLSL] map lerp to Fmix (PR #88976)

2024-04-16 Thread Cooper Partin via cfe-commits

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

Looks good to me.

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


[clang] [llvm] [HLSL] move rcp to cgbuiltins (PR #88401)

2024-04-11 Thread Cooper Partin via cfe-commits

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

Looks good to me

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


[clang] [HLSL] Implement array temporary support (PR #79382)

2024-03-29 Thread Cooper Partin via cfe-commits

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

WOW.. This was pretty big.  I went through it twice and didn't see anything 
that jumped out.  Looks good to me.

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


[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

2024-03-26 Thread Cooper Partin via cfe-commits

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

Looks good to me for what I can see in the code.

I took special care to make sure places where noticed the old size accessor 
(CAT->getSize().getZExtValue() ) being called, was now using 
(CAT->getZExtSize()).


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


[clang] [CMake][HLSL] Add SPIRV to target list for build (PR #86323)

2024-03-22 Thread Cooper Partin via cfe-commits

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

SPIRV in the house!

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


[clang] [llvm] [DXIL] `exp`, `any`, `lerp`, & `rcp` Intrinsic Lowering (PR #84526)

2024-03-15 Thread Cooper Partin via cfe-commits


@@ -0,0 +1,186 @@
+//===- DXILIntrinsicExpansion.cpp - Prepare LLVM Module for DXIL 
encoding--===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+///
+/// \file This file contains DXIL intrinsic expansions for those that don't 
have
+//  opcodes in DirectX Intermediate Language (DXIL).
+//===--===//
+
+#include "DXILIntrinsicExpansion.h"
+#include "DirectX.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/CodeGen/Passes.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/Instruction.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/Intrinsics.h"
+#include "llvm/IR/IntrinsicsDirectX.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/PassManager.h"
+#include "llvm/IR/Type.h"
+#include "llvm/Pass.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/MathExtras.h"
+
+#define DEBUG_TYPE "dxil-intrinsic-expansion"
+
+using namespace llvm;
+
+static bool isIntrinsicExpansion(Function &F) {
+  switch (F.getIntrinsicID()) {
+  case Intrinsic::exp:
+  case Intrinsic::dx_any:
+  case Intrinsic::dx_lerp:
+  case Intrinsic::dx_rcp:
+return true;
+  }
+  return false;
+}
+
+static bool expandExpIntrinsic(CallInst *Orig) {
+  Value *X = Orig->getOperand(0);
+  IRBuilder<> Builder(Orig->getParent());
+  Builder.SetInsertPoint(Orig);
+  Type *Ty = X->getType();
+  Type *EltTy = Ty->getScalarType();
+  Constant *Log2eConst =
+  Ty->isVectorTy() ? ConstantVector::getSplat(
+ ElementCount::getFixed(
+ cast(Ty)->getNumElements()),
+ ConstantFP::get(EltTy, numbers::log2e))
+   : ConstantFP::get(EltTy, numbers::log2e);
+  Value *NewX = Builder.CreateFMul(Log2eConst, X);
+  auto *Exp2Call =
+  Builder.CreateIntrinsic(Ty, Intrinsic::exp2, {NewX}, nullptr, "dx.exp2");
+  Exp2Call->setTailCall(Orig->isTailCall());
+  Exp2Call->setAttributes(Orig->getAttributes());
+  Orig->replaceAllUsesWith(Exp2Call);
+  Orig->eraseFromParent();
+  return true;
+}
+
+static bool expandAnyIntrinsic(CallInst *Orig) {
+  Value *X = Orig->getOperand(0);
+  IRBuilder<> Builder(Orig->getParent());
+  Builder.SetInsertPoint(Orig);
+  Type *Ty = X->getType();
+  Type *EltTy = Ty->getScalarType();
+
+  if (!Ty->isVectorTy()) {
+Value *Cond = EltTy->isFloatingPointTy()
+  ? Builder.CreateFCmpUNE(X, ConstantFP::get(EltTy, 0))
+  : Builder.CreateICmpNE(X, ConstantInt::get(EltTy, 0));
+Orig->replaceAllUsesWith(Cond);
+  } else {
+auto *XVec = dyn_cast(Ty);
+Value *Cond =
+EltTy->isFloatingPointTy()
+? Builder.CreateFCmpUNE(
+  X, ConstantVector::getSplat(
+ ElementCount::getFixed(XVec->getNumElements()),
+ ConstantFP::get(EltTy, 0)))
+: Builder.CreateICmpNE(
+  X, ConstantVector::getSplat(
+ ElementCount::getFixed(XVec->getNumElements()),
+ ConstantInt::get(EltTy, 0)));
+Value *Result = Builder.CreateExtractElement(Cond, (uint64_t)0);
+for (unsigned I = 1; I < XVec->getNumElements(); I++) {
+  Value *Elt = Builder.CreateExtractElement(Cond, I);
+  Result = Builder.CreateOr(Result, Elt);
+}
+Orig->replaceAllUsesWith(Result);
+  }
+  Orig->eraseFromParent();
+  return true;
+}
+
+static bool expandLerpIntrinsic(CallInst *Orig) {
+  Value *X = Orig->getOperand(0);
+  Value *Y = Orig->getOperand(1);
+  Value *S = Orig->getOperand(2);
+  IRBuilder<> Builder(Orig->getParent());
+  Builder.SetInsertPoint(Orig);
+  auto *V = Builder.CreateFSub(Y, X);
+  V = Builder.CreateFMul(S, V);
+  auto *Result = Builder.CreateFAdd(X, V, "dx.lerp");
+  Orig->replaceAllUsesWith(Result);
+  Orig->eraseFromParent();
+  return true;
+}
+
+static bool expandRcpIntrinsic(CallInst *Orig) {
+  Value *X = Orig->getOperand(0);
+  IRBuilder<> Builder(Orig->getParent());
+  Builder.SetInsertPoint(Orig);
+  Type *Ty = X->getType();
+  Type *EltTy = Ty->getScalarType();
+  Constant *One =
+  Ty->isVectorTy()
+  ? ConstantVector::getSplat(
+ElementCount::getFixed(
+dyn_cast(Ty)->getNumElements()),
+ConstantFP::get(EltTy, 1.0))
+  : ConstantFP::get(EltTy, 1.0);
+  auto *Result = Builder.CreateFDiv(One, X, "dx.rcp");
+  Orig->replaceAllUsesWith(Result);
+  Orig->eraseFromParent();
+  return true;
+}
+
+static bool expandIntrinsic(Function &F, CallInst *Orig) {
+  switch (F.getIntrinsicID()) {
+  case Intrinsic::exp:
+return expandExpIntrinsic(Orig);
+  case Intrinsic::dx_any:
+

[clang] [llvm] [DXIL] `exp`, `any`, `lerp`, & `rcp` Intrinsic Lowering (PR #84526)

2024-03-15 Thread Cooper Partin via cfe-commits

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

Looks good to me for what I can understand.

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


[clang] [llvm] [DXIL] `exp`, `any`, `lerp`, & `rcp` Intrinsic Lowering (PR #84526)

2024-03-15 Thread Cooper Partin via cfe-commits

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


[clang] [DirectX] Fix HLSL bitshifts to leverage the OpenCL pipeline for bitshifting (PR #81030)

2024-02-08 Thread Cooper Partin via cfe-commits

coopp wrote:

> What is the output before this change? It would be nice to add more 
> information to the PR description.

I added a description that shows a before/after output for this change.

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


[clang] [DirectX] Fix HLSL bitshifts to leverage the OpenCL pipeline for bitshifting (PR #81030)

2024-02-08 Thread Cooper Partin via cfe-commits

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


[clang] [DirectX] Fix HLSL bitshifts to leverage the OpenCL pipeline for bitshifting (PR #81030)

2024-02-08 Thread Cooper Partin via cfe-commits

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


[clang] [DirectX] Fix HLSL bitshifts to leverage the OpenCL pipeline for bitshifting Fixes #55106 In HLSL bit shifts are defined to shift by shift size % type size. This contains the following changes

2024-02-08 Thread Cooper Partin via cfe-commits

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


[clang] [DirectX] Fix HLSL bitshifts to leverage the OpenCL pipeline for bitshifting (PR #81030)

2024-02-08 Thread Cooper Partin via cfe-commits

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


[clang] [DirectX] Fix HLSL bitshifts to leverage the OpenCL pipeline for bitshifting (#55106) (PR #81030)

2024-02-08 Thread Cooper Partin via cfe-commits

https://github.com/coopp updated https://github.com/llvm/llvm-project/pull/81030

>From eb6c7e6e9d4ccaf6e63714d2d94a518cc00bcc82 Mon Sep 17 00:00:00 2001
From: Cooper Partin 
Date: Wed, 7 Feb 2024 11:32:56 -0800
Subject: [PATCH 1/2] [DirectX] Fix HLSL bitshifts to leverage the OpenCL
 pipeline for bitshift masking

---
 clang/lib/CodeGen/CGExprScalar.cpp |  4 +--
 clang/test/CodeGenHLSL/shift-mask.hlsl | 35 ++
 2 files changed, 37 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/shift-mask.hlsl

diff --git a/clang/lib/CodeGen/CGExprScalar.cpp 
b/clang/lib/CodeGen/CGExprScalar.cpp
index df8f71cf1d9008..fa03163bbde577 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -4168,7 +4168,7 @@ Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
   bool SanitizeBase = SanitizeSignedBase || SanitizeUnsignedBase;
   bool SanitizeExponent = CGF.SanOpts.has(SanitizerKind::ShiftExponent);
   // OpenCL 6.3j: shift values are effectively % word size of LHS.
-  if (CGF.getLangOpts().OpenCL)
+  if (CGF.getLangOpts().OpenCL || CGF.getLangOpts().HLSL)
 RHS = ConstrainShiftValue(Ops.LHS, RHS, "shl.mask");
   else if ((SanitizeBase || SanitizeExponent) &&
isa(Ops.LHS->getType())) {
@@ -4237,7 +4237,7 @@ Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
 
   // OpenCL 6.3j: shift values are effectively % word size of LHS.
-  if (CGF.getLangOpts().OpenCL)
+  if (CGF.getLangOpts().OpenCL || CGF.getLangOpts().HLSL)
 RHS = ConstrainShiftValue(Ops.LHS, RHS, "shr.mask");
   else if (CGF.SanOpts.has(SanitizerKind::ShiftExponent) &&
isa(Ops.LHS->getType())) {
diff --git a/clang/test/CodeGenHLSL/shift-mask.hlsl 
b/clang/test/CodeGenHLSL/shift-mask.hlsl
new file mode 100644
index 00..b8a7dced1a0ee5
--- /dev/null
+++ b/clang/test/CodeGenHLSL/shift-mask.hlsl
@@ -0,0 +1,35 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN:   dxil-pc-shadermodel6.3-library %s \
+// RUN:   -emit-llvm -disable-llvm-passes -o - | FileCheck %s
+
+int shl32(int V, int S) {
+  return V << S;
+}
+
+// CHECK: define noundef i32 @"?shl32{{[@$?.A-Za-z0-9_]+}}"(i32 noundef %V, 
i32 noundef %S) #0 {
+// CHECK-DAG:  %[[Masked:.*]] = and i32 %{{.*}}, 31
+// CHECK-DAG:  %{{.*}} = shl i32 %{{.*}}, %[[Masked]]
+
+int shr32(int V, int S) {
+  return V >> S;
+}
+
+// CHECK: define noundef i32 @"?shr32{{[@$?.A-Za-z0-9_]+}}"(i32 noundef %V, 
i32 noundef %S) #0 {
+// CHECK-DAG:  %[[Masked:.*]] = and i32 %{{.*}}, 31
+// CHECK-DAG:  %{{.*}} = ashr i32 %{{.*}}, %[[Masked]]
+
+int64_t shl64(int64_t V, int64_t S) {
+  return V << S;
+}
+
+// CHECK: define noundef i64 @"?shl64{{[@$?.A-Za-z0-9_]+}}"(i64 noundef %V, 
i64 noundef %S) #0 {
+// CHECK-DAG:  %[[Masked:.*]] = and i64 %{{.*}}, 63
+// CHECK-DAG:  %{{.*}} = shl i64 %{{.*}}, %[[Masked]]
+
+int64_t shr64(int64_t V, int64_t S) {
+  return V >> S;
+}
+
+// CHECK: define noundef i64 @"?shr64{{[@$?.A-Za-z0-9_]+}}"(i64 noundef %V, 
i64 noundef %S) #0 {
+// CHECK-DAG:  %[[Masked:.*]] = and i64 %{{.*}}, 63
+// CHECK-DAG:  %{{.*}} = ashr i64 %{{.*}}, %[[Masked]]
\ No newline at end of file

>From 10e4cf84afe41cbc5508b914605e0817c5b62509 Mon Sep 17 00:00:00 2001
From: Cooper Partin 
Date: Thu, 8 Feb 2024 08:59:48 -0800
Subject: [PATCH 2/2] Add newline to end of .hlsl test file

---
 clang/test/CodeGenHLSL/shift-mask.hlsl | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/CodeGenHLSL/shift-mask.hlsl 
b/clang/test/CodeGenHLSL/shift-mask.hlsl
index b8a7dced1a0ee5..d046efaf9c1f9c 100644
--- a/clang/test/CodeGenHLSL/shift-mask.hlsl
+++ b/clang/test/CodeGenHLSL/shift-mask.hlsl
@@ -32,4 +32,4 @@ int64_t shr64(int64_t V, int64_t S) {
 
 // CHECK: define noundef i64 @"?shr64{{[@$?.A-Za-z0-9_]+}}"(i64 noundef %V, 
i64 noundef %S) #0 {
 // CHECK-DAG:  %[[Masked:.*]] = and i64 %{{.*}}, 63
-// CHECK-DAG:  %{{.*}} = ashr i64 %{{.*}}, %[[Masked]]
\ No newline at end of file
+// CHECK-DAG:  %{{.*}} = ashr i64 %{{.*}}, %[[Masked]]

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [DirectX] Fix HLSL bitshifts to leverage the OpenCL pipeline for bitshifting (#55106) (PR #81030)

2024-02-07 Thread Cooper Partin via cfe-commits

https://github.com/coopp created https://github.com/llvm/llvm-project/pull/81030

None

>From eb6c7e6e9d4ccaf6e63714d2d94a518cc00bcc82 Mon Sep 17 00:00:00 2001
From: Cooper Partin 
Date: Wed, 7 Feb 2024 11:32:56 -0800
Subject: [PATCH] [DirectX] Fix HLSL bitshifts to leverage the OpenCL pipeline
 for bitshift masking

---
 clang/lib/CodeGen/CGExprScalar.cpp |  4 +--
 clang/test/CodeGenHLSL/shift-mask.hlsl | 35 ++
 2 files changed, 37 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/shift-mask.hlsl

diff --git a/clang/lib/CodeGen/CGExprScalar.cpp 
b/clang/lib/CodeGen/CGExprScalar.cpp
index df8f71cf1d9008..fa03163bbde577 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -4168,7 +4168,7 @@ Value *ScalarExprEmitter::EmitShl(const BinOpInfo &Ops) {
   bool SanitizeBase = SanitizeSignedBase || SanitizeUnsignedBase;
   bool SanitizeExponent = CGF.SanOpts.has(SanitizerKind::ShiftExponent);
   // OpenCL 6.3j: shift values are effectively % word size of LHS.
-  if (CGF.getLangOpts().OpenCL)
+  if (CGF.getLangOpts().OpenCL || CGF.getLangOpts().HLSL)
 RHS = ConstrainShiftValue(Ops.LHS, RHS, "shl.mask");
   else if ((SanitizeBase || SanitizeExponent) &&
isa(Ops.LHS->getType())) {
@@ -4237,7 +4237,7 @@ Value *ScalarExprEmitter::EmitShr(const BinOpInfo &Ops) {
 RHS = Builder.CreateIntCast(RHS, Ops.LHS->getType(), false, "sh_prom");
 
   // OpenCL 6.3j: shift values are effectively % word size of LHS.
-  if (CGF.getLangOpts().OpenCL)
+  if (CGF.getLangOpts().OpenCL || CGF.getLangOpts().HLSL)
 RHS = ConstrainShiftValue(Ops.LHS, RHS, "shr.mask");
   else if (CGF.SanOpts.has(SanitizerKind::ShiftExponent) &&
isa(Ops.LHS->getType())) {
diff --git a/clang/test/CodeGenHLSL/shift-mask.hlsl 
b/clang/test/CodeGenHLSL/shift-mask.hlsl
new file mode 100644
index 00..b8a7dced1a0ee5
--- /dev/null
+++ b/clang/test/CodeGenHLSL/shift-mask.hlsl
@@ -0,0 +1,35 @@
+// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple \
+// RUN:   dxil-pc-shadermodel6.3-library %s \
+// RUN:   -emit-llvm -disable-llvm-passes -o - | FileCheck %s
+
+int shl32(int V, int S) {
+  return V << S;
+}
+
+// CHECK: define noundef i32 @"?shl32{{[@$?.A-Za-z0-9_]+}}"(i32 noundef %V, 
i32 noundef %S) #0 {
+// CHECK-DAG:  %[[Masked:.*]] = and i32 %{{.*}}, 31
+// CHECK-DAG:  %{{.*}} = shl i32 %{{.*}}, %[[Masked]]
+
+int shr32(int V, int S) {
+  return V >> S;
+}
+
+// CHECK: define noundef i32 @"?shr32{{[@$?.A-Za-z0-9_]+}}"(i32 noundef %V, 
i32 noundef %S) #0 {
+// CHECK-DAG:  %[[Masked:.*]] = and i32 %{{.*}}, 31
+// CHECK-DAG:  %{{.*}} = ashr i32 %{{.*}}, %[[Masked]]
+
+int64_t shl64(int64_t V, int64_t S) {
+  return V << S;
+}
+
+// CHECK: define noundef i64 @"?shl64{{[@$?.A-Za-z0-9_]+}}"(i64 noundef %V, 
i64 noundef %S) #0 {
+// CHECK-DAG:  %[[Masked:.*]] = and i64 %{{.*}}, 63
+// CHECK-DAG:  %{{.*}} = shl i64 %{{.*}}, %[[Masked]]
+
+int64_t shr64(int64_t V, int64_t S) {
+  return V >> S;
+}
+
+// CHECK: define noundef i64 @"?shr64{{[@$?.A-Za-z0-9_]+}}"(i64 noundef %V, 
i64 noundef %S) #0 {
+// CHECK-DAG:  %[[Masked:.*]] = and i64 %{{.*}}, 63
+// CHECK-DAG:  %{{.*}} = ashr i64 %{{.*}}, %[[Masked]]
\ No newline at end of file

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits