Skip to content

Commit

Permalink
Merge pull request #709 from KhronosGroup/clang-ci
Browse files Browse the repository at this point in the history
Add/Run/CI Clang-format on adoc/code
  • Loading branch information
gmlueck authored Feb 13, 2025
2 parents 6e30d0c + 147b939 commit 4c87cbc
Show file tree
Hide file tree
Showing 32 changed files with 364 additions and 143 deletions.
27 changes: 19 additions & 8 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
Language: Cpp
BasedOnStyle: LLVM
AccessModifierOffset: -1
BreakInheritanceList: BeforeComma
BreakConstructorInitializers: BeforeComma
Cpp11BracedListStyle: false
PointerAlignment: Left
SpaceBeforeCpp11BracedList: true
---
Language: Cpp
BasedOnStyle: Google
AlignTrailingComments: true
AllowShortBlocksOnASingleLine: false
AllowShortIfStatementsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakTemplateDeclarations: true
BreakConstructorInitializersBeforeComma: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
# Do not adapt pointer and reference alignment to the current style.
# Just use PointerAlignment parameter unconditionally for consistency.
DerivePointerAlignment: false
Standard: c++17
SpacesInParentheses: false
SpacesInAngles: false
SpaceInEmptyParentheses: false
SpacesInCStyleCastParentheses: false
SortIncludes: true
15 changes: 15 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,18 @@ jobs:
name: spec-outputs
path: |
/tmp/out
check-clang-format:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0 # Need to check out base branch as well
- name: Install clang-format
run: sudo apt update; sudo apt install -y clang-format-18
# Inspired by SYCL-CTS
- name: Run clang-format on changed files from adoc/code/
run: |
git diff -U0 --no-color ${{ github.event.pull_request.base.sha }} ./adoc/code/ | ./adoc/scripts/clang-format-diff.py -p1
12 changes: 6 additions & 6 deletions adoc/code/algorithms.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
// Copyright (c) 2011-2024 The Khronos Group, Inc.
// SPDX-License-Identifier: Apache-2.0

buffer<int> inputBuf { 1024 };
buffer<int> outputBuf { 2 };
buffer<int> inputBuf{1024};
buffer<int> outputBuf{2};
{
// Initialize buffer on the host with 0, 1, 2, 3, ..., 1023
host_accessor a { inputBuf };
host_accessor a{inputBuf};
std::iota(a.begin(), a.end(), 0);
}

myQueue.submit([&](handler& cgh) {
accessor inputValues { inputBuf, cgh, read_only };
accessor outputValues { outputBuf, cgh, write_only, no_init };
accessor inputValues{inputBuf, cgh, read_only};
accessor outputValues{outputBuf, cgh, write_only, no_init};

cgh.parallel_for(nd_range<1>(range<1>(16), range<1>(16)), [=](nd_item<1> it) {
// Apply a group algorithm to any number of values, described by an iterator
Expand All @@ -31,5 +31,5 @@ myQueue.submit([&](handler& cgh) {
});
});

host_accessor a { outputBuf };
host_accessor a{outputBuf};
assert(a[0] == 523776 && a[1] == 120);
14 changes: 7 additions & 7 deletions adoc/code/anatomy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

#include <iostream>
#include <sycl/sycl.hpp>
using namespace sycl; // (optional) avoids need for "sycl::" before SYCL names
using namespace sycl; // (optional) avoids need for "sycl::" before SYCL names

int main() {
int data[1024]; // Allocate data to be worked on
int data[1024]; // Allocate data to be worked on

// Create a default queue to enqueue work to the default device
queue myQueue;
Expand All @@ -16,20 +16,20 @@ int main() {
// because the destructor of resultBuf will wait
{
// Wrap our data variable in a buffer
buffer<int, 1> resultBuf { data, range<1> { 1024 } };
buffer<int, 1> resultBuf{data, range<1>{1024}};

// Create a command group to issue commands to the queue
myQueue.submit([&](handler& cgh) {
// Request write access to the buffer without initialization
accessor writeResult { resultBuf, cgh, write_only, no_init };
accessor writeResult{resultBuf, cgh, write_only, no_init};

// Enqueue a parallel_for task with 1024 work-items
cgh.parallel_for(1024, [=](id<1> idx) {
// Initialize each buffer element with its own rank number starting at 0
writeResult[idx] = idx;
}); // End of the kernel function
}); // End of our commands for this queue
} // End of scope, so we wait for work producing resultBuf to complete
}); // End of the kernel function
}); // End of our commands for this queue
} // End of scope, so we wait for work producing resultBuf to complete

// Print result
for (int i = 0; i < 1024; i++)
Expand Down
11 changes: 5 additions & 6 deletions adoc/code/aspectTraitExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// SPDX-License-Identifier: MIT

#include <sycl/sycl.hpp>
using namespace sycl; // (optional) avoids need for "sycl::" before SYCL names
using namespace sycl; // (optional) avoids need for "sycl::" before SYCL names

constexpr int N = 512;

template <bool HasFp16> class MyKernel {
template <bool HasFp16>
class MyKernel {
public:
void operator()(id<1> i) {
if constexpr (HasFp16) {
Expand All @@ -22,11 +23,9 @@ int main() {
myQueue.submit([&](handler& cgh) {
device dev = myQueue.get_device();
if (dev.has(aspect::fp16)) {
cgh.parallel_for(range { N },
MyKernel<any_device_has_v<aspect::fp16>> {});
cgh.parallel_for(range{N}, MyKernel<any_device_has_v<aspect::fp16>>{});
} else {
cgh.parallel_for(range { N },
MyKernel<all_devices_have_v<aspect::fp16>> {});
cgh.parallel_for(range{N}, MyKernel<all_devices_have_v<aspect::fp16>>{});
}
});

Expand Down
2 changes: 1 addition & 1 deletion adoc/code/basicParallelForItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

myQueue.submit([&](handler& cgh) {
accessor acc { myBuffer, cgh, write_only };
accessor acc{myBuffer, cgh, write_only};

cgh.parallel_for(range<1>(numWorkItems), [=](item<1> item) {
// kernel argument type is item
Expand Down
2 changes: 1 addition & 1 deletion adoc/code/basicparallelfor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

myQueue.submit([&](handler& cgh) {
accessor acc { myBuffer, cgh, write_only };
accessor acc{myBuffer, cgh, write_only};

cgh.parallel_for(range<1>(numWorkItems),
[=](id<1> index) { acc[index] = 42.0f; });
Expand Down
6 changes: 3 additions & 3 deletions adoc/code/bundle-builtin-kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

#include <sycl/sycl.hpp>
using namespace sycl; // (optional) avoids need for "sycl::" before SYCL names
using namespace sycl; // (optional) avoids need for "sycl::" before SYCL names

int main() {
queue myQueue;
Expand All @@ -15,7 +15,7 @@ int main() {
// Get an executable kernel_bundle containing all the built-in kernels
// supported by the device.
kernel_bundle<bundle_state::executable> myBundle =
get_kernel_bundle(myContext, { myDevice }, builtinKernelIds);
get_kernel_bundle(myContext, {myDevice}, builtinKernelIds);

// Retrieve a kernel object that can be used to query for more information
// about the built-in kernel or to submit it to a command group. We assume
Expand All @@ -26,7 +26,7 @@ int main() {
myQueue.submit([&](handler& cgh) {
// Setting the arguments depends on the backend and the exact kernel used.
cgh.set_args(...);
cgh.parallel_for(range { 1024 }, builtinKernel);
cgh.parallel_for(range{1024}, builtinKernel);
});

myQueue.wait();
Expand Down
12 changes: 6 additions & 6 deletions adoc/code/bundle-kernel-introspection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// SPDX-License-Identifier: Apache-2.0

#include <sycl/sycl.hpp>
using namespace sycl; // (optional) avoids need for "sycl::" before SYCL names
using namespace sycl; // (optional) avoids need for "sycl::" before SYCL names

class MyKernel; // Forward declare the name of our kernel.
class MyKernel; // Forward declare the name of our kernel.

int main() {
size_t N = 1024;
Expand All @@ -15,7 +15,7 @@ int main() {
// Get an executable kernel bundle containing our kernel.
kernel_id kernelId = get_kernel_id<MyKernel>();
auto myBundle =
get_kernel_bundle<bundle_state::executable>(myContext, { kernelId });
get_kernel_bundle<bundle_state::executable>(myContext, {kernelId});

// Get the kernel's maximum work-group size when running on our device.
kernel myKernel = myBundle.get_kernel(kernelId);
Expand All @@ -24,11 +24,11 @@ int main() {

// Compute a good ND-range to use for iteration in the kernel
// based on the maximum work-group size.
std::array<size_t, 11> divisors = { 1024, 512, 256, 128, 64, 32,
16, 8, 4, 2, 1 };
std::array<size_t, 11> divisors = {1024, 512, 256, 128, 64, 32,
16, 8, 4, 2, 1};
size_t wgSize = *std::find_if(divisors.begin(), divisors.end(),
[=](auto d) { return (d <= maxWgSize); });
nd_range myRange { range { N }, range { wgSize } };
nd_range myRange{range{N}, range{wgSize}};

myQueue.submit([&](handler& cgh) {
// Use the kernel bundle we queried, so we are sure the queried work-group
Expand Down
4 changes: 2 additions & 2 deletions adoc/code/bundle-pre-compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

#include <sycl/sycl.hpp>
using namespace sycl; // (optional) avoids need for "sycl::" before SYCL names
using namespace sycl; // (optional) avoids need for "sycl::" before SYCL names

int main() {
queue myQueue;
Expand All @@ -18,7 +18,7 @@ int main() {
// pre-compiled kernel from "myBundle".
cgh.use_kernel_bundle(myBundle);

cgh.parallel_for(range { 1024 }, ([=](item index) {
cgh.parallel_for(range{1024}, ([=](item index) {
// kernel code
}));
});
Expand Down
8 changes: 4 additions & 4 deletions adoc/code/bundle-spec-constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

#include <sycl/sycl.hpp>
using namespace sycl; // (optional) avoids need for "sycl::" before SYCL names
using namespace sycl; // (optional) avoids need for "sycl::" before SYCL names

// Forward declare names for our two kernels.
class MyKernel1;
Expand All @@ -21,7 +21,7 @@ int main() {

// Get the identifiers for our kernels, then get an input kernel bundle that
// contains our two kernels.
auto kernelIds = { get_kernel_id<MyKernel1>(), get_kernel_id<MyKernel2>() };
auto kernelIds = {get_kernel_id<MyKernel1>(), get_kernel_id<MyKernel2>()};
auto inputBundle =
get_kernel_bundle<bundle_state::input>(myContext, kernelIds);

Expand All @@ -37,7 +37,7 @@ int main() {
// Use the kernel bundle we built in this command group.
cgh.use_kernel_bundle(exeBundle);
cgh.parallel_for<MyKernel1>(
range { 1024 }, ([=](item index, kernel_handler kh) {
range{1024}, ([=](item index, kernel_handler kh) {
// Read the value of the specialization constant.
int w = kh.get_specialization_constant<width>();
// ...
Expand All @@ -48,7 +48,7 @@ int main() {
// This command group uses the same kernel bundle.
cgh.use_kernel_bundle(exeBundle);
cgh.parallel_for<MyKernel2>(
range { 1024 }, ([=](item index, kernel_handler kh) {
range{1024}, ([=](item index, kernel_handler kh) {
int h = kh.get_specialization_constant<height>();
// ...
}));
Expand Down
8 changes: 4 additions & 4 deletions adoc/code/deviceHas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class KernelFunctor {

private:
void foo() const {
half fp = 1.0; // No compiler diagnostic here
half fp = 1.0; // No compiler diagnostic here
}

void bar() const {
sycl::atomic_ref longAtomic(longValue);
longAtomic.fetchAdd(1); // ERROR: Compiler issues diagnostic because
// "aspect::atomic64" missing from "device_has()"
longAtomic.fetchAdd(1); // ERROR: Compiler issues diagnostic because
// "aspect::atomic64" missing from "device_has()"
}
};

Expand All @@ -25,5 +25,5 @@ class KernelFunctor {
// still check the device's aspects before submitting the kernel.
if (myQueue.get_device().has(aspect::fp16)) {
myQueue.submit(
[&](handler& h) { h.parallel_for(range { 16 }, KernelFunctor {}); });
[&](handler& h) { h.parallel_for(range{16}, KernelFunctor{}); });
}
6 changes: 3 additions & 3 deletions adoc/code/explicitcopy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
const size_t nElems = 10u;

// Create a vector and fill it with values 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
std::vector<int> v { nElems };
std::vector<int> v{nElems};
std::iota(std::begin(v), std::end(v), 0);

// Create a buffer with no associated user storage
sycl::buffer<int, 1> b { range<1>(nElems) };
sycl::buffer<int, 1> b{range<1>(nElems)};

// Create a queue
queue myQueue;

myQueue.submit([&](handler& cgh) {
// Retrieve a ranged write accessor to a global buffer with access to the
// first half of the buffer
accessor acc { b, cgh, range<1>(nElems / 2), id<1>(0), write_only };
accessor acc{b, cgh, range<1>(nElems / 2), id<1>(0), write_only};
// Copy the first five elements of the vector into the buffer associated with
// the accessor
cgh.copy(v.data(), acc);
Expand Down
10 changes: 6 additions & 4 deletions adoc/code/lambdaNameExamples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
// Explicit kernel names can be optionally forward declared at namespace scope
class MyForwardDeclName;

template <typename T> class MyTemplatedKernelName;
template <typename T>
class MyTemplatedKernelName;

// Define and launch templated kernel
template <typename T> void templatedFunction() {
template <typename T>
void templatedFunction() {
queue myQueue;

// Launch A: No explicit kernel name
Expand Down Expand Up @@ -47,9 +49,9 @@ int main() {
});
});

templatedFunction<int>(); // OK
templatedFunction<int>(); // OK

templatedFunction<std::complex<float>>(); // Launch A is OK, Launch B illegal
templatedFunction<std::complex<float>>(); // Launch A is OK, Launch B illegal
// because std::complex is not forward declarable according to C++, and was
// used in an explicit kernel name which must be forward declarable.
}
Loading

0 comments on commit 4c87cbc

Please sign in to comment.