Skip to content

[NFC][SYCL] Refactor SpecConstantsPass #10613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 4, 2023

Conversation

maksimsab
Copy link
Contributor

@maksimsab maksimsab commented Jul 28, 2023

To improve readability of SpecConstantsPass::run function, some of its content is outlined into a separate helper functions.

@maksimsab maksimsab requested a review from AlexeySachkov July 28, 2023 16:00
@maksimsab maksimsab requested a review from a team as a code owner July 28, 2023 16:00
@maksimsab maksimsab temporarily deployed to aws July 28, 2023 16:20 — with GitHub Actions Inactive
@maksimsab maksimsab temporarily deployed to aws July 28, 2023 17:01 — with GitHub Actions Inactive
/// %A = type { i32 }
/// @value = constant %"spec_id" { %A { i32 1 } }, align 4
/// call spir_func void @getCompositeSpecConst(%1, %2, @value, %3)
static Constant *tryGetSpecConstInitializerFromCI(CallInst *CI,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With SYCL 2020, we must always have a default value available, which means that the function should not be called try, we should not use dyn_cast<GlobalVariable>, but instead use cast, etc. The latter could be a bit of a problem in case there is a complex chain of instructions leading from the argument to a global variable, but it should be compile-time resolvable with something like llvm::getUnderlyingObject, for example.

Once again, it is probably better to have this change in a separate PR, because it may cause some regressions if we miss some corner cases. Better not mix it with a non-functional refactoring, but let's have a TODO comment here somewhere about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With SYCL 2020, we must always have a default value available

But here we check that sycl-post-link is able to come up with default value from type

; This test checks that composite specialization constants can be correctly
; initialized by sycl-post-link tool for AOT use-case (default initialization
; should be used according to the type of constant)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is likely an outdated/legacy test case and the comment. Yes, old 1.2.1 extension implementation used to set default values based on type in AOT mode, because there were no way to set them in source code.

With SYCL 2020, default value is defined by user when constructing specialization_id object:

Effects: Constructs a specialization_id containing an instance of T initialized with args..., which represents the specialization constant’s default value.

And the value is guaranteed to be compile-time known:

the specialization_id variable must be declared as constexpr;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this test.

/// Function replaces last Metadata node in the given vector with new
/// node which contains given Padding.
static void
updatePaddingInLastMDNode(LLVMContext &Ctx,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to the PR, just an observation: we have at least one more place where we insert some padding metadata:

SpecConstantDescriptor Desc;
// ID of padding descriptors is the max value possible. This value is a
// magic value for the runtime and will just be skipped. Even if there
// are many specialization constants and every constant has padding of
// a different length, everything will work regardless rewriting
// the descriptions with Desc.ID equals to the max value: they will just
// be ignored at all.
Desc.ID = std::numeric_limits<unsigned>::max();
Desc.Offset = LocalOffset;
Desc.Size = PostStructPadding;
Result.push_back(Desc);

These paddings are hard and its been a while since I looked into this code. It would be good to refresh that knowledge to see if we could unify adding those paddings or at least add more explanatory comments, because right now I'm not sure that I understand why do we have both those places where we add special descriptors

@maksimsab maksimsab temporarily deployed to aws August 2, 2023 13:23 — with GitHub Actions Inactive
@maksimsab maksimsab temporarily deployed to aws August 2, 2023 14:04 — with GitHub Actions Inactive
@maksimsab maksimsab requested a review from AlexeySachkov August 3, 2023 13:21
@maksimsab maksimsab temporarily deployed to aws August 3, 2023 13:42 — with GitHub Actions Inactive
@maksimsab maksimsab temporarily deployed to aws August 3, 2023 14:22 — with GitHub Actions Inactive
@AlexeySachkov AlexeySachkov changed the title [NFC][SYCL] Refactor SpecConstPass. Part 1 [NFC][SYCL] Refactor SpecConstantsPass Aug 4, 2023
@AlexeySachkov AlexeySachkov merged commit c1bb379 into intel:sycl Aug 4, 2023
@maksimsab maksimsab deleted the refactor_function branch August 9, 2023 13:16
mdtoguchi pushed a commit to mdtoguchi/llvm that referenced this pull request Oct 18, 2023
To improve readability of `SpecConstantsPass::run` function, some of its
content is outlined into a separate helper functions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants