-
Notifications
You must be signed in to change notification settings - Fork 2
implementation of external intrinsics with processing & replacment header content #235
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
Open
vyast-softserveinc
wants to merge
10
commits into
devel
Choose a base branch
from
external_intrinsics
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cb62078
implementation of external intrinsics with processing & replacment he…
d4c5f00
finished implementation for external intrinsics & added test suits fo…
74cdbd8
delete removed old folder with buildin_headers
1562354
remove unused function from builtin_intrinsics
5c8e368
remove comment of TODO
82bda0c
fix forexternal intrinsic generate wrong code for launcher & fix seri…
e7b7aca
added luancher part to be implemented in case of external intrinsic
96b0912
make the launcher external intrinsics to use embed strategy
adece8c
reimplement external intrinsic idea to make it embeded into final tra…
a3bc8aa
remove commented code
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1131,4 +1131,4 @@ secring.* | |
| *build* | ||
|
|
||
| .vscode/ | ||
| lib/core/builtin_headers/okl_intrinsic_* | ||
| lib/core/intrinsics/okl_intrinsic_* | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 5 additions & 5 deletions
10
lib/core/builtin_headers/intrinsic_impl.cpp → lib/core/intrinsics/builtin_intrinsics.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| #include "core/intrinsics/external_intrinsics.h" | ||
|
|
||
| #include <clang/Frontend/CompilerInstance.h> | ||
| #include "core/transpiler_session/transpiler_session.h" | ||
| #include "util/string_utils.hpp" | ||
|
|
||
| #include <algorithm> | ||
|
|
||
| namespace oklt { | ||
|
|
||
| using namespace llvm; | ||
| namespace fs = std::filesystem; | ||
|
|
||
| tl::expected<fs::path, std::string> | ||
| getIntrincisImplSourcePath(TargetBackend backend, | ||
| const fs::path &intrincisPath) | ||
| { | ||
| switch (backend) { | ||
| case TargetBackend::CUDA: | ||
| return intrincisPath / "cuda"; | ||
| case TargetBackend::HIP: | ||
| return intrincisPath / "hip"; | ||
| case TargetBackend::DPCPP: | ||
| return intrincisPath / "dpcpp"; | ||
| case TargetBackend::OPENMP: | ||
| return intrincisPath / "openmp"; | ||
| case TargetBackend::SERIAL: | ||
| return intrincisPath / "serial"; | ||
| default: | ||
| return tl::make_unexpected("User intrinsic does not implement target backend"); | ||
| } | ||
| } | ||
|
|
||
| bool isExternalInstrincisInclude(TranspilerSession &session, | ||
| const std::string &fileName) | ||
| { | ||
| auto maybeUserIntrinsic = session.getInput().userIntrincis; | ||
| if(!maybeUserIntrinsic) { | ||
| return false; | ||
| } | ||
| auto instrinsic = maybeUserIntrinsic.value(); | ||
| auto folderPrefix = instrinsic.filename().string(); | ||
| return util::starts_with(fileName, folderPrefix); | ||
| } | ||
|
|
||
|
|
||
| tl::expected<std::unique_ptr<llvm::MemoryBuffer>, std::string> | ||
| getExternalIntrinsicSource(TargetBackend backend, | ||
| const fs::path &intrinsicPath, | ||
| clang::SourceManager& sm) | ||
| { | ||
|
|
||
| auto implPathResult = getIntrincisImplSourcePath(backend, intrinsicPath); | ||
| if(!implPathResult) { | ||
| return tl::make_unexpected(implPathResult.error()); | ||
| } | ||
|
|
||
| auto sourceFolder = implPathResult.value(); | ||
| if(!std::filesystem::exists(sourceFolder)) { | ||
| return tl::make_unexpected("Intrinsic implementation folder does not exist"); | ||
| } | ||
|
|
||
| std::vector<fs::path> files(fs::directory_iterator(sourceFolder), {}); | ||
| if(files.empty()) { | ||
| return tl::make_unexpected("Intrinsic implementation files is missing"); | ||
| } | ||
|
|
||
| auto it = std::find_if(files.cbegin(), files.cend(), [](const fs::path &p) -> bool { | ||
| return p.extension().string() == std::string(".h"); | ||
| }); | ||
|
|
||
| if(it == files.cend()) { | ||
| std::string error = "Can't' find implementation file with path: " + sourceFolder.string(); | ||
| return tl::make_unexpected(error); | ||
| } | ||
|
|
||
| auto &fm = sm.getFileManager(); | ||
| auto backendFilePath = it->string(); | ||
| auto maybeReplacedFile = fm.getFile(backendFilePath); | ||
| if(!maybeReplacedFile) { | ||
| std::string error = "Can't open file: " + backendFilePath; | ||
| return tl::make_unexpected(error); | ||
| } | ||
| auto maybeBuffer = fm.getBufferForFile(maybeReplacedFile.get()); | ||
| if (!maybeBuffer) { | ||
| std::string error = "Can't get memory buffer for: " + backendFilePath; | ||
| return tl::make_unexpected(error); | ||
| } | ||
| return std::move(maybeBuffer.get()); | ||
| } | ||
|
|
||
| void overrideExternalIntrinsic(TranspilerSession &session, | ||
| const std::string &includedFileName, | ||
| clang::OptionalFileEntryRef includedFile, | ||
| clang::SourceManager &sourceManager) | ||
| { | ||
| auto maybeIntrinsicPath = session.getInput().userIntrincis; | ||
| if(maybeIntrinsicPath && isExternalInstrincisInclude(session, includedFileName)) { | ||
| auto intrinsicPath = maybeIntrinsicPath.value(); | ||
| auto infoResult = getExternalIntrinsicSource(session.getInput().backend, intrinsicPath, sourceManager); | ||
| if(!infoResult) { | ||
| session.pushError(std::error_code(), infoResult.error()); | ||
| return; | ||
| } | ||
| auto buffer = std::move(infoResult.value()); | ||
| auto &fm = sourceManager.getFileManager(); | ||
| if(includedFile) { | ||
| auto fileRef = includedFile; | ||
| const auto &fileEntry = fileRef->getFileEntry(); | ||
| sourceManager.overrideFileContents(&fileEntry, std::move(buffer)); | ||
| } else { | ||
| //INFO: case when the file can be found by relative path | ||
| // it happens when the include path is relative to WORKING DIR path | ||
| auto maybeFileRef = fm.getFileRef(includedFileName); | ||
| if(maybeFileRef) { | ||
| auto foundFileRef = maybeFileRef.get(); | ||
| sourceManager.overrideFileContents(foundFileRef, std::move(buffer)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace oklt | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #pragma once | ||
| #include <string> | ||
| #include <clang/Basic/FileEntry.h> | ||
|
|
||
| namespace clang { | ||
| class CompilerInstance; | ||
| class SourceManager; | ||
| } | ||
|
|
||
| namespace oklt { | ||
|
|
||
| class TranspilerSession; | ||
|
|
||
| //bool isExternalInstrincisInclude(TranspilerSession &session, | ||
vyast-softserveinc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // const std::string &fileName); | ||
|
|
||
| void overrideExternalIntrinsic(TranspilerSession &session, | ||
| const std::string &includedFileName, | ||
| clang::OptionalFileEntryRef includedFile, | ||
| clang::SourceManager &sourceManager); | ||
|
|
||
| } // namespace oklt | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.