Skip to content

Commit 7383011

Browse files
author
Pavel V Chupin
committed
Merge from 'main' to 'sycl-web' (17 commits)
CONFLICT (content): Merge conflict in clang/lib/Sema/SemaDeclAttr.cpp
2 parents 3196d3c + 3f0587d commit 7383011

File tree

94 files changed

+1710
-21327
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+1710
-21327
lines changed

clang/docs/HLSLSupport.rst

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
============
2+
HLSL Support
3+
============
4+
5+
.. contents::
6+
:local:
7+
8+
Introduction
9+
============
10+
11+
HLSL Support is under active development in the Clang codebase. This document
12+
describes the high level goals of the project, the guiding principles, as well
13+
as some idiosyncrasies of the HLSL language and how we intend to support them in
14+
Clang.
15+
16+
Project Goals
17+
=============
18+
19+
The long term goal of this project is to enable Clang to function as a
20+
replacement for the `DirectXShaderCompiler (DXC)
21+
<https://github.com/microsoft/DirectXShaderCompiler/>`_ in all its supported
22+
use cases. Accomplishing that goal will require Clang to be able to process most
23+
existing HLSL programs with a high degree of source compatibility.
24+
25+
Non-Goals
26+
---------
27+
28+
HLSL ASTs do not need to be compatible between DXC and Clang. We do not expect
29+
identical code generation or that features will resemble DXC's implementation or
30+
architecture. In fact, we explicitly expect to deviate from DXC's implementation
31+
in key ways.
32+
33+
Guiding Principles
34+
==================
35+
36+
This document lacks details for architectural decisions that are not yet
37+
finalized. Our top priorities are quality, maintainability, and flexibility. In
38+
accordance with community standards we are expecting a high level of test
39+
coverage, and we will engineer our solutions with long term maintenance in mind.
40+
We are also working to limit modifications to the Clang C++ code paths and
41+
share as much functionality as possible.
42+
43+
Architectural Direction
44+
=======================
45+
46+
HLSL support in Clang is expressed as C++ minus unsupported C and C++ features.
47+
This is different from how other Clang languages are implemented. Most languages
48+
in Clang are additive on top of C.
49+
50+
HLSL is not a formally or fully specified language, and while our goals require
51+
a high level of source compatibility, implementations can vary and we have some
52+
flexibility to be more or less permissive in some cases. For modern HLSL DXC is
53+
the reference implementation.
54+
55+
The HLSL effort prioritizes following similar patterns for other languages,
56+
drivers, runtimes and targets. Specifically, We will maintain separation between
57+
HSLS-specific code and the rest of Clang as much as possible following patterns
58+
in use in Clang code today (i.e. ParseHLSL.cpp, SemaHLSL.cpp, CGHLSL*.cpp...).
59+
We will use inline checks on language options where the code is simple and
60+
isolated, and prefer HLSL-specific implementation files for any code of
61+
reasonable complexity.
62+
63+
In places where the HLSL language is in conflict with C and C++, we will seek to
64+
make minimally invasive changes guarded under the HLSL language options. We will
65+
seek to make HLSL language support as minimal a maintenance burden as possible.
66+
67+
DXC Driver
68+
----------
69+
70+
A DXC driver mode will provide command-line compatibility with DXC, supporting
71+
DXC's options and flags. The DXC driver is HLSL-specific and will create an
72+
HLSLToolchain which will provide the basis to support targeting both DirectX and
73+
Vulkan.
74+
75+
Parser
76+
------
77+
78+
Following the examples of other parser extensions HLSL will add a ParseHLSL.cpp
79+
file to contain the implementations of HLSL-specific extensions to the Clang
80+
parser. The HLSL grammar shares most of its structure with C and C++, so we will
81+
use the existing C/C++ parsing code paths.
82+
83+
Sema
84+
----
85+
86+
HLSL's Sema implementation will also provide an ``ExternalSemaSource``. In DXC,
87+
an ``ExternalSemaSource`` is used to provide definitions for HLSL built-in data
88+
types and built-in templates. Clang is already designed to allow an attached
89+
``ExternalSemaSource`` to lazily complete data types, which is a **huge**
90+
performance win for HLSL.
91+
92+
CodeGen
93+
-------
94+
95+
Like OpenCL, HLSL relies on capturing a lot of information into IR metadata.
96+
*hand wave* *hand wave* *hand wave* As a design principle here we want our IR to
97+
be idiomatic Clang IR as much as possible. We will use IR attributes wherever we
98+
can, and use metadata as sparingly as possible. One example of a difference from
99+
DXC already implemented in Clang is the use of target triples to communicate
100+
shader model versions and shader stages.
101+
102+
Our HLSL CodeGen implementation should also have an eye toward generating IR
103+
that will map directly to targets other than DXIL. While IR itself is generally
104+
not re-targetable, we want to share the Clang CodeGen implementation for HLSL
105+
with other GPU graphics targets like SPIR-V and possibly other GPU and even CPU
106+
targets.
107+
108+
HLSL Language
109+
=============
110+
111+
The HLSL language is insufficiently documented, and not formally specified.
112+
Documentation is available on `Microsoft's website
113+
<https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl>`_.
114+
The language syntax is similar enough to C and C++ that carefully written C and
115+
C++ code is valid HLSL. HLSL has some key differences from C & C++ which we will
116+
need to handle in Clang.
117+
118+
HLSL is not a conforming or valid extension or superset of C or C++. The
119+
language has key incompatibilities with C and C++, both syntactically and
120+
semantically.
121+
122+
An Aside on GPU Languages
123+
-------------------------
124+
125+
Due to HLSL being a GPU targeted language HLSL is a Single Program Multiple Data
126+
(SPMD) language relying on the implicit parallelism provided by GPU hardware.
127+
Some language features in HLSL enable programmers to take advantage of the
128+
parallel nature of GPUs in a hardware abstracted language.
129+
130+
HLSL also prohibits some features of C and C++ which can have catastrophic
131+
performance or are not widely supportable on GPU hardware or drivers. As an
132+
example, register spilling is often excessively expensive on GPUs, so HLSL
133+
requires all functions to be inlined during code generation, and does not
134+
support a runtime calling convention.
135+
136+
Pointers & References
137+
---------------------
138+
139+
HLSL does not support referring to values by address. Semantically all variables
140+
are value-types and behave as such. HLSL disallows the pointer dereference
141+
operators (unary ``*``, and ``->``), as well as the address of operator (unary
142+
&). While HLSL disallows pointers and references in the syntax, HLSL does use
143+
reference types in the AST, and we intend to use pointer decay in the AST in
144+
the Clang implementation.
145+
146+
HLSL ``this`` Keyword
147+
---------------------
148+
149+
HLSL does support member functions, and (in HLSL 2021) limited operator
150+
overloading. With member function support, HLSL also has a ``this`` keyword. The
151+
``this`` keyword is an example of one of the places where HLSL relies on
152+
references in the AST, because ``this`` is a reference.
153+
154+
Bitshifts
155+
---------
156+
157+
In deviation from C, HLSL bitshifts are defined to mask the shift count by the
158+
size of the type. In DXC, the semantics of LLVM IR were altered to accommodate
159+
this, in Clang we intend to generate the mask explicitly in the IR. In cases
160+
where the shift value is constant, this will be constant folded appropriately,
161+
in other cases we can clean it up in the DXIL target.
162+
163+
Non-short Circuiting Logical Operators
164+
--------------------------------------
165+
166+
In HLSL 2018 and earlier, HLSL supported logical operators (and the ternary
167+
operator) on vector types. This behavior required that operators not short
168+
circuit. The non-short circuiting behavior applies to all data types until HLSL
169+
2021. In HLSL 2021, logical and ternary operators do not support vector types
170+
instead builtin functions ``and``, ``or`` and ``select`` are available, and
171+
operators short circuit matching C behavior.
172+
173+
Precise Qualifier
174+
-----------------
175+
176+
HLSL has a ``precise`` qualifier that behaves unlike anything else in the C
177+
language. The support for this qualifier in DXC is buggy, so our bar for
178+
compatibility is low.
179+
180+
The ``precise`` qualifier applies in the inverse direction from normal
181+
qualifiers. Rather than signifying that the declaration containing ``precise``
182+
qualifier be precise, it signifies that the operations contributing to the
183+
declaration's value be ``precise``. Additionally, ``precise`` is a misnomer:
184+
values attributed as ``precise`` comply with IEEE-754 floating point semantics,
185+
and are prevented from optimizations which could decrease *or increase*
186+
precision.
187+
188+
Differences in Templates
189+
------------------------
190+
191+
HLSL uses templates to define builtin types and methods, but disallowed
192+
user-defined templates until HLSL 2021. HLSL also allows omitting empty template
193+
parameter lists when all template parameters are defaulted. This is an ambiguous
194+
syntax in C++, but Clang detects the case and issues a diagnostic. This makes
195+
supporting the case in Clang minimally invasive.
196+
197+
Vector Extensions
198+
-----------------
199+
200+
HLSL uses the OpenCL vector extensions, and also provides C++-style constructors
201+
for vectors that are not supported by Clang.
202+
203+
Standard Library
204+
----------------
205+
206+
HLSL does not support the C or C++ standard libraries. Like OpenCL, HLSL
207+
describes its own library of built in types, complex data types, and functions.
208+
209+
Unsupported C & C++ Features
210+
----------------------------
211+
212+
HLSL does not support all features of C and C++. In implementing HLSL in Clang
213+
use of some C and C++ features will produce diagnostics under HLSL, and others
214+
will be supported as language extensions. In general, any C or C++ feature that
215+
can be supported by the DXIL and SPIR-V code generation targets could be treated
216+
as a clang HLSL extension. Features that cannot be lowered to DXIL or SPIR-V,
217+
must be diagnosed as errors.
218+
219+
HLSL does not support the following C features:
220+
221+
* Pointers
222+
* References
223+
* ``goto`` or labels
224+
* Variable Length Arrays
225+
* ``_Complex`` and ``_Imaginary``
226+
* C Threads or Atomics (or Obj-C blocks)
227+
* ``union`` types `(in progress for HLSL 202x) <https://github.com/microsoft/DirectXShaderCompiler/pull/4132>`_
228+
* Most features C11 and later
229+
230+
HLSL does not support the following C++ features:
231+
232+
* RTTI
233+
* Exceptions
234+
* Multiple inheritance
235+
* Access specifiers
236+
* Anonymous or inline namespaces
237+
* ``new`` & ``delete`` operators in all of their forms (array, placement, etc)
238+
* Constructors and destructors
239+
* Any use of the ``virtual`` keyword
240+
* Most features C++11 and later

clang/docs/ReleaseNotes.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ Major New Features
5454
There is an analogous ``zero_call_used_regs`` attribute to allow for finer
5555
control of this feature.
5656

57+
- Clang now supports randomizing structure layout in C. This feature is a
58+
compile-time hardening technique, making it more difficult for an attacker to
59+
retrieve data from structures. Specify randomization with the
60+
``randomize_layout`` attribute. The corresponding ``no_randomize_layout``
61+
attribute can be used to turn the feature off.
62+
63+
A seed value is required to enable randomization, and is deterministic based
64+
on a seed value. Use the ``-frandomize-layout-seed=`` or
65+
``-frandomize-layout-seed-file=`` flags.
66+
67+
.. note::
68+
69+
Randomizing structure layout is a C-only feature.
70+
5771
Bug Fixes
5872
------------------
5973
- ``CXXNewExpr::getArraySize()`` previously returned a ``llvm::Optional``

clang/docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Using Clang as a Compiler
4545
OpenCLSupport
4646
OpenMPSupport
4747
SYCLSupport
48+
HLSLSupport
4849
ThinLTO
4950
APINotes
5051
CommandGuide/index

clang/include/clang/AST/Decl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4052,6 +4052,12 @@ class RecordDecl : public TagDecl {
40524052
RecordDeclBits.ParamDestroyedInCallee = V;
40534053
}
40544054

4055+
bool isRandomized() const { return RecordDeclBits.IsRandomized; }
4056+
4057+
void setIsRandomized(bool V) { RecordDeclBits.IsRandomized = V; }
4058+
4059+
void reorderFields(const SmallVectorImpl<Decl *> &Fields);
4060+
40554061
/// Determines whether this declaration represents the
40564062
/// injected class name.
40574063
///

clang/include/clang/AST/DeclBase.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ class alignas(8) Decl {
313313
friend class ASTReader;
314314
friend class CXXClassMemberWrapper;
315315
friend class LinkageComputer;
316+
friend class RecordDecl;
316317
template<typename decl_type> friend class Redeclarable;
317318

318319
/// Access - Used by C++ decls for the access specifier.
@@ -1540,10 +1541,13 @@ class DeclContext {
15401541

15411542
/// Represents the way this type is passed to a function.
15421543
uint64_t ArgPassingRestrictions : 2;
1544+
1545+
/// Indicates whether this struct has had its field layout randomized.
1546+
uint64_t IsRandomized : 1;
15431547
};
15441548

15451549
/// Number of non-inherited bits in RecordDeclBitfields.
1546-
enum { NumRecordDeclBits = 14 };
1550+
enum { NumRecordDeclBits = 15 };
15471551

15481552
/// Stores the bits used by OMPDeclareReductionDecl.
15491553
/// If modified NumOMPDeclareReductionDeclBits and the accessor

clang/include/clang/AST/Randstruct.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//===- Randstruct.h - Interfact for structure randomization -------*- C++ -*-=//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file contains the interface for Clang's structure field layout
10+
// randomization.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_CLANG_AST_RANDSTRUCT_H
15+
#define LLVM_CLANG_AST_RANDSTRUCT_H
16+
17+
namespace llvm {
18+
template <typename T> class ArrayRef;
19+
template <typename T> class SmallVectorImpl;
20+
class StringRef;
21+
} // end namespace llvm
22+
23+
namespace clang {
24+
25+
class ASTContext;
26+
class Decl;
27+
class RecordDecl;
28+
29+
namespace randstruct {
30+
31+
bool randomizeStructureLayout(const ASTContext &Context, llvm::StringRef Name,
32+
llvm::ArrayRef<Decl *> Fields,
33+
llvm::SmallVectorImpl<Decl *> &FinalOrdering);
34+
35+
} // namespace randstruct
36+
} // namespace clang
37+
38+
#endif // LLVM_CLANG_AST_RANDSTRUCT_H

clang/include/clang/Basic/Attr.td

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4859,3 +4859,18 @@ def HLSLNumThreads: InheritableAttr {
48594859
let LangOpts = [HLSL];
48604860
let Documentation = [NumThreadsDocs];
48614861
}
4862+
4863+
def RandomizeLayout : InheritableAttr {
4864+
let Spellings = [GCC<"randomize_layout">];
4865+
let Subjects = SubjectList<[Record]>;
4866+
let Documentation = [ClangRandomizeLayoutDocs];
4867+
let LangOpts = [COnly];
4868+
}
4869+
4870+
def NoRandomizeLayout : InheritableAttr {
4871+
let Spellings = [GCC<"no_randomize_layout">];
4872+
let Subjects = SubjectList<[Record]>;
4873+
let Documentation = [ClangRandomizeLayoutDocs];
4874+
let LangOpts = [COnly];
4875+
}
4876+
def : MutualExclusions<[RandomizeLayout, NoRandomizeLayout]>;

clang/include/clang/Basic/AttrDocs.td

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7957,3 +7957,35 @@ dictate the thread id. Total number of threads executed is ``X * Y * Z``.
79577957
The full documentation is available here: https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-attributes-numthreads
79587958
}];
79597959
}
7960+
7961+
def ClangRandomizeLayoutDocs : Documentation {
7962+
let Category = DocCatDecl;
7963+
let Heading = "randomize_layout, no_randomize_layout";
7964+
let Content = [{
7965+
The attribute ``randomize_layout``, when attached to a C structure, selects it
7966+
for structure layout field randomization; a compile-time hardening technique. A
7967+
"seed" value, is specified via the ``-frandomize-layout-seed=`` command line flag.
7968+
For example:
7969+
7970+
.. code-block:: bash
7971+
7972+
SEED=`od -A n -t x8 -N 32 /dev/urandom | tr -d ' \n'`
7973+
make ... CFLAGS="-frandomize-layout-seed=$SEED" ...
7974+
7975+
You can also supply the seed in a file with ``-frandomize-layout-seed-file=``.
7976+
For example:
7977+
7978+
.. code-block:: bash
7979+
7980+
od -A n -t x8 -N 32 /dev/urandom | tr -d ' \n' > /tmp/seed_file.txt
7981+
make ... CFLAGS="-frandomize-layout-seed-file=/tmp/seed_file.txt" ...
7982+
7983+
The randomization is deterministic based for a given seed, so the entire
7984+
program should be compiled with the same seed, but keep the seed safe
7985+
otherwise.
7986+
7987+
The attribute ``no_randomize_layout``, when attached to a C structure,
7988+
instructs the compiler that this structure should not have its field layout
7989+
randomized.
7990+
}];
7991+
}

0 commit comments

Comments
 (0)