Skip to content

Commit 263effe

Browse files
committed
Merge branch 'sk/hlsl_6' of https://github.com/soufianekhiat/Halide into sk/hlsl_6
2 parents 380d8f2 + 7e38017 commit 263effe

File tree

6 files changed

+132
-43
lines changed

6 files changed

+132
-43
lines changed

.github/workflows/clang-tidy.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on:
77
- '**.c'
88
- '**.cpp'
99
- 'run-clang-tidy.sh'
10+
- 'tools/clang-tidy-filter.sh'
1011
- '**.clang-tidy'
1112
- '.github/workflows/clang-tidy.yml'
1213

run-clang-tidy.sh

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,32 +128,18 @@ jq -s 'add' "${CLANG_TIDY_BUILD_DIR}/compile_commands.json" \
128128
>"${CLANG_TIDY_BUILD_DIR}/compile_commands_merged.json"
129129
mv "${CLANG_TIDY_BUILD_DIR}/compile_commands_merged.json" "${CLANG_TIDY_BUILD_DIR}/compile_commands.json"
130130

131-
# Wrapper filters noisy "N warnings generated." from each clang-tidy invocation.
132-
CLANG_TIDY_FILTER="${CLANG_TIDY_BUILD_DIR}/clang-tidy-filter.sh"
133-
cat >"${CLANG_TIDY_FILTER}" <<WRAPPER
134-
#!/usr/bin/env bash
135-
"${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/clang-tidy" "\$@" 2>&1 | grep -v '^[[:digit:]]\+ warnings\? generated\.\$'
136-
exit "\${PIPESTATUS[0]}"
137-
WRAPPER
138-
chmod +x "${CLANG_TIDY_FILTER}"
131+
echo "Running clang-tidy..."
139132

140-
echo Running clang-tidy...
141133
export PYTHONUNBUFFERED=1
134+
export CLANG_TIDY_REAL_BINARY="${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/clang-tidy"
135+
export CLANG_TIDY_ROOT_DIR="${ROOT_DIR}"
142136
"${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/run-clang-tidy" \
143137
${FIX} \
144138
-j "${J}" \
145139
-quiet \
146140
-p "${CLANG_TIDY_BUILD_DIR}" \
147-
-clang-tidy-binary "${CLANG_TIDY_FILTER}" \
141+
-clang-tidy-binary "${ROOT_DIR}/tools/clang-tidy-filter.sh" \
148142
-clang-apply-replacements-binary "${CLANG_TIDY_LLVM_INSTALL_DIR}/bin/clang-apply-replacements" \
149143
"$@"
150144

151-
CLANG_TIDY_EXIT_CODE=$?
152-
153-
if [ "$CLANG_TIDY_EXIT_CODE" -eq 0 ]; then
154-
echo "Success!"
155-
else
156-
echo "clang-tidy failed with exit code $CLANG_TIDY_EXIT_CODE"
157-
fi
158-
159-
exit "$CLANG_TIDY_EXIT_CODE"
145+
echo "Success!"

src/CodeGen_D3D12Compute_Dev.cpp

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ class CodeGen_D3D12Compute_Dev : public CodeGen_GPU_Dev {
148148
void visit(const FloatImm *op) override;
149149

150150
Scope<> groupshared_allocations;
151+
bool emit_precise = false;
151152
};
152153

153154
std::ostringstream src_stream;
@@ -527,6 +528,12 @@ void CodeGen_D3D12Compute_Dev::CodeGen_D3D12Compute_C::visit(const Call *op) {
527528
// directly.
528529
Expr equiv = Call::make(op->type, "pow", op->args, Call::PureExtern);
529530
equiv.accept(this);
531+
} else if (op->is_strict_float_intrinsic()) {
532+
ScopedValue old_emit_precise(emit_precise, true);
533+
Expr equiv = op->is_intrinsic(Call::strict_fma) ?
534+
Call::make(op->type, "fma", op->args, Call::PureExtern) :
535+
unstrictify_float(op);
536+
equiv.accept(this);
530537
} else if (op->is_intrinsic(Call::round)) {
531538
// HLSL's round intrinsic has the correct semantics for our rounding.
532539
Expr equiv = Call::make(op->type, "round", op->args, Call::PureExtern);
@@ -942,8 +949,12 @@ void CodeGen_D3D12Compute_Dev::CodeGen_D3D12Compute_C::visit(const Store *op) {
942949
// Detect min/max/bitwise patterns by examining the expression structure.
943950
// Each has the form op(load, rhs) where rhs is independent of the buffer.
944951
auto detect_rhs = [&](const Expr &a, const Expr &b) -> Expr {
945-
if (equal(a, equiv_load) && !expr_uses_var(b, op->name)) { return b; }
946-
if (equal(b, equiv_load) && !expr_uses_var(a, op->name)) { return a; }
952+
if (equal(a, equiv_load) && !expr_uses_var(b, op->name)) {
953+
return b;
954+
}
955+
if (equal(b, equiv_load) && !expr_uses_var(a, op->name)) {
956+
return a;
957+
}
947958
return Expr();
948959
};
949960
Expr rhs;
@@ -1108,6 +1119,14 @@ void CodeGen_D3D12Compute_Dev::CodeGen_D3D12Compute_C::visit(const Store *op) {
11081119
}
11091120

11101121
void CodeGen_D3D12Compute_Dev::CodeGen_D3D12Compute_C::visit(const Select *op) {
1122+
if (op->type.is_vector()) {
1123+
// A vector of bool was recursively introduced while
1124+
// performing codegen. Eliminate it.
1125+
Expr equiv = eliminate_bool_vectors(op);
1126+
equiv.accept(this);
1127+
return;
1128+
}
1129+
11111130
ostringstream rhs;
11121131
string true_val = print_expr(op->true_value);
11131132
string false_val = print_expr(op->false_value);
@@ -1177,7 +1196,25 @@ void CodeGen_D3D12Compute_Dev::CodeGen_D3D12Compute_C::visit(const Free *op) {
11771196

11781197
string CodeGen_D3D12Compute_Dev::CodeGen_D3D12Compute_C::print_assignment(Type type, const string &rhs) {
11791198
string rhs_modified = print_reinforced_cast(type, rhs);
1180-
return CodeGen_GPU_C::print_assignment(type, rhs_modified);
1199+
const char *precise_flag = (type.is_float() && emit_precise) ? "precise " : "";
1200+
const string cache_key = precise_flag + rhs_modified;
1201+
1202+
auto cached = cache.find(cache_key);
1203+
if (cached == cache.end()) {
1204+
id = unique_name('_');
1205+
const char *const_flag = output_kind == CPlusPlusImplementation ? " const " : "";
1206+
if (type.is_handle()) {
1207+
// Don't print void *, which might lose useful type information. just use auto.
1208+
stream << get_indent() << "auto *";
1209+
} else {
1210+
stream << get_indent() << precise_flag << print_type(type, AppendSpace);
1211+
}
1212+
stream << const_flag << id << " = " << rhs_modified << ";\n";
1213+
cache[cache_key] = id;
1214+
} else {
1215+
id = cached->second;
1216+
}
1217+
return id;
11811218
}
11821219

11831220
string CodeGen_D3D12Compute_Dev::CodeGen_D3D12Compute_C::print_vanilla_cast(Type type, const string &value_expr) {
@@ -1826,13 +1863,12 @@ void CodeGen_D3D12Compute_Dev::init_module() {
18261863
#endif
18271864
// DXC (SM 6.x) rejects FXC-specific float literals 1.#IND / 1.#INF.
18281865
// Use asfloat() with IEEE 754 bit patterns instead.
1829-
<< (sm >= 60
1830-
? "float nan_f32() { return asfloat(0x7fc00000u); } \n"
1831-
"float neg_inf_f32() { return asfloat(0xff800000u); } \n"
1832-
"float inf_f32() { return asfloat(0x7f800000u); } \n"
1833-
: "float nan_f32() { return 1.#IND; } \n"
1834-
"float neg_inf_f32() { return -1.#INF; } \n"
1835-
"float inf_f32() { return +1.#INF; } \n")
1866+
<< (sm >= 60 ? "float nan_f32() { return asfloat(0x7fc00000u); } \n"
1867+
"float neg_inf_f32() { return asfloat(0xff800000u); } \n"
1868+
"float inf_f32() { return asfloat(0x7f800000u); } \n" :
1869+
"float nan_f32() { return 1.#IND; } \n"
1870+
"float neg_inf_f32() { return -1.#INF; } \n"
1871+
"float inf_f32() { return +1.#INF; } \n")
18361872
<< "#define float_from_bits asfloat \n"
18371873
// pow() in HLSL has the same semantics as C if
18381874
// x > 0. Otherwise, we need to emulate C

src/runtime/d3d12compute.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@
7676

7777
WEAK void d3d12_debug_dump();
7878

79-
#define d3d12_panic(...) \
80-
do { \
81-
/* Print the fatal message via halide_print (non-aborting) FIRST so it \
82-
is always visible, even though d3d12_debug_dump() aborts below. */ \
79+
#define d3d12_panic(...) \
80+
do { \
81+
/* Print the fatal message via halide_print (non-aborting) FIRST so it \
82+
is always visible, even though d3d12_debug_dump() aborts below. */ \
8383
BasicPrinter<4096>(nullptr) << "D3D12 FATAL: " << __VA_ARGS__ << "\n" \
84-
<< "vvvvv D3D12 Begin Debug Dump vvvvv\n"; \
85-
d3d12_debug_dump(); /* aborts via halide_error inside */ \
84+
<< "vvvvv D3D12 Begin Debug Dump vvvvv\n"; \
85+
d3d12_debug_dump(); /* aborts via halide_error inside */ \
8686
} while (0)
8787

8888
// v trace and logging utilities for debugging v
@@ -2032,7 +2032,9 @@ WEAK bool D3D12LoadDXC(void *uc) {
20322032
// Trim to the directory portion (last backslash).
20332033
char *last_sep = path;
20342034
for (char *p = path; *p; p++) {
2035-
if (*p == '\\' || *p == '/') last_sep = p;
2035+
if (*p == '\\' || *p == '/') {
2036+
last_sep = p;
2037+
}
20362038
}
20372039
char *dst = last_sep + 1;
20382040
for (const char *src = "dxcompiler.dll"; *src;) {

src/runtime/mini_dxc.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,13 @@ MIDL_INTERFACE("E5204DC7-D18C-4C3C-BDFB-851673980FE7")
289289
IDxcLibrary : public IUnknown {
290290
public:
291291
virtual HRESULT STDMETHODCALLTYPE SetMalloc(void *pMalloc) = 0;
292-
virtual HRESULT STDMETHODCALLTYPE CreateBlobFromBlob(IDxcBlob *pBlob, UINT32 offset, UINT32 length,
293-
_COM_Outptr_ IDxcBlob **ppResult) = 0;
294-
virtual HRESULT STDMETHODCALLTYPE CreateBlobFromFile(LPCWSTR pFileName, UINT32 *pCodePage,
295-
_COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) = 0;
292+
virtual HRESULT STDMETHODCALLTYPE CreateBlobFromBlob(IDxcBlob * pBlob, UINT32 offset, UINT32 length,
293+
_COM_Outptr_ IDxcBlob * *ppResult) = 0;
294+
virtual HRESULT STDMETHODCALLTYPE CreateBlobFromFile(LPCWSTR pFileName, UINT32 * pCodePage,
295+
_COM_Outptr_ IDxcBlobEncoding * *pBlobEncoding) = 0;
296296
virtual HRESULT STDMETHODCALLTYPE CreateBlobWithEncodingFromPinned(
297297
LPCVOID pText, UINT32 size, UINT32 codePage,
298-
_COM_Outptr_ IDxcBlobEncoding **pBlobEncoding) = 0;
298+
_COM_Outptr_ IDxcBlobEncoding * *pBlobEncoding) = 0;
299299
};
300300

301301
#endif /* C++ only — C-style vtable omitted (not used) */
@@ -311,11 +311,11 @@ MIDL_INTERFACE("8C210BF3-011F-4422-8D70-6F9ACB374E6F")
311311
IDxcCompiler : public IUnknown {
312312
public:
313313
virtual HRESULT STDMETHODCALLTYPE Compile(
314-
IDxcBlob *pSource,
314+
IDxcBlob * pSource,
315315
_In_opt_ LPCWSTR pSourceName,
316316
LPCWSTR pEntryPoint,
317317
LPCWSTR pTargetProfile,
318-
_In_opt_count_(argCount) LPCWSTR *pArguments,
318+
_In_opt_count_(argCount) LPCWSTR * pArguments,
319319
UINT32 argCount,
320320
_In_opt_ const void *pDefines,
321321
UINT32 defineCount,

tools/clang-tidy-filter.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env bash
2+
3+
set -o pipefail
4+
5+
if [[ -z ${CLANG_TIDY_REAL_BINARY:-} ]]; then
6+
echo "CLANG_TIDY_REAL_BINARY must be set" 1>&2
7+
exit 1
8+
fi
9+
10+
if [[ -z ${CLANG_TIDY_ROOT_DIR:-} ]]; then
11+
echo "CLANG_TIDY_ROOT_DIR must be set" 1>&2
12+
exit 1
13+
fi
14+
15+
escape_github_annotation_value() {
16+
local value="$1"
17+
value="${value//'%'/%25}"
18+
value="${value//$'\r'/%0D}"
19+
value="${value//$'\n'/%0A}"
20+
value="${value//','/%2C}"
21+
value="${value//':'/%3A}"
22+
printf '%s' "${value}"
23+
}
24+
25+
OUTPUT_FILE=$(mktemp)
26+
"${CLANG_TIDY_REAL_BINARY}" "$@" >"${OUTPUT_FILE}" 2>&1
27+
CLANG_TIDY_STATUS=$?
28+
29+
grep -v '^[[:digit:]]\+ warnings\? generated\.$' "${OUTPUT_FILE}"
30+
31+
if [[ ${GITHUB_ACTIONS} == "true" ]]; then
32+
while IFS= read -r line; do
33+
if [[ ${line} =~ ^(.+):([0-9]+):([0-9]+):[[:space:]](warning|error|note):[[:space:]](.*)\ \[([^][]+)\]$ ]]; then
34+
file="${BASH_REMATCH[1]}"
35+
line_no="${BASH_REMATCH[2]}"
36+
col_no="${BASH_REMATCH[3]}"
37+
severity="${BASH_REMATCH[4]}"
38+
message="${BASH_REMATCH[5]}"
39+
check_name="${BASH_REMATCH[6]}"
40+
elif [[ ${line} =~ ^(.+):([0-9]+):([0-9]+):[[:space:]](warning|error|note):[[:space:]](.*)$ ]]; then
41+
file="${BASH_REMATCH[1]}"
42+
line_no="${BASH_REMATCH[2]}"
43+
col_no="${BASH_REMATCH[3]}"
44+
severity="${BASH_REMATCH[4]}"
45+
message="${BASH_REMATCH[5]}"
46+
check_name="clang-tidy"
47+
else
48+
continue
49+
fi
50+
51+
if [[ ${file} == "${CLANG_TIDY_ROOT_DIR}/"* ]]; then
52+
file="${file#"${CLANG_TIDY_ROOT_DIR}/"}"
53+
fi
54+
55+
file="$(escape_github_annotation_value "${file}")"
56+
title="$(escape_github_annotation_value "${check_name}")"
57+
message="$(escape_github_annotation_value "${message}")"
58+
59+
echo "::${severity} file=${file},line=${line_no},col=${col_no},title=${title}::${message}"
60+
done <"${OUTPUT_FILE}"
61+
fi
62+
63+
rm -f "${OUTPUT_FILE}"
64+
exit "${CLANG_TIDY_STATUS}"

0 commit comments

Comments
 (0)