-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add helper functions to query properties of the lowered Target (#8192) #8359
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
122ff30
Add helper functions to query properties of the lowered Target (#8192)
steven-johnson 1f3ac01
Add Python bindings
steven-johnson a5550ea
clang-format
steven-johnson 548c0ad
clang-format
steven-johnson c1c81f8
Add comments
steven-johnson 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 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 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 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 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 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 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 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 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 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,54 @@ | ||
#include "TargetQueryOps.h" | ||
|
||
#include "Function.h" | ||
#include "IRMutator.h" | ||
#include "IROperator.h" | ||
|
||
namespace Halide { | ||
namespace Internal { | ||
|
||
namespace { | ||
|
||
class LowerTargetQueryOps : public IRMutator { | ||
const Target &t; | ||
|
||
using IRMutator::visit; | ||
|
||
Expr visit(const Call *call) override { | ||
if (call->is_intrinsic(Call::target_arch_is)) { | ||
Target::Arch arch = (Target::Arch)*as_const_int(call->args[0]); | ||
return make_bool(t.arch == arch); | ||
} else if (call->is_intrinsic(Call::target_has_feature)) { | ||
Target::Feature feat = (Target::Feature)*as_const_int(call->args[0]); | ||
return make_bool(t.has_feature(feat)); | ||
} else if (call->is_intrinsic(Call::target_natural_vector_size)) { | ||
Expr zero = call->args[0]; | ||
return Expr(t.natural_vector_size(zero.type())); | ||
} else if (call->is_intrinsic(Call::target_os_is)) { | ||
Target::OS os = (Target::OS)*as_const_int(call->args[0]); | ||
return make_bool(t.os == os); | ||
} else if (call->is_intrinsic(Call::target_bits)) { | ||
return Expr(t.bits); | ||
} | ||
|
||
return IRMutator::visit(call); | ||
} | ||
|
||
public: | ||
LowerTargetQueryOps(const Target &t) | ||
: t(t) { | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
void lower_target_query_ops(std::map<std::string, Function> &env, const Target &t) { | ||
for (auto &iter : env) { | ||
Function &func = iter.second; | ||
LowerTargetQueryOps ltqo(t); | ||
func.mutate(<qo); | ||
} | ||
} | ||
|
||
} // namespace Internal | ||
} // namespace Halide |
This file contains 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,24 @@ | ||
#ifndef HALIDE_TARGET_QUERY_OPS_H | ||
#define HALIDE_TARGET_QUERY_OPS_H | ||
|
||
/** \file | ||
* Defines a lowering pass to lower all target_is() and target_has() helpers. | ||
*/ | ||
|
||
#include <map> | ||
#include <string> | ||
|
||
namespace Halide { | ||
|
||
struct Target; | ||
|
||
namespace Internal { | ||
|
||
class Function; | ||
|
||
void lower_target_query_ops(std::map<std::string, Function> &env, const Target &t); | ||
|
||
} // namespace Internal | ||
} // namespace Halide | ||
|
||
#endif |
This file contains 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 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,48 @@ | ||
#include "Halide.h" | ||
#include <stdio.h> | ||
|
||
using namespace Halide; | ||
|
||
int main(int argc, char **argv) { | ||
// For simplicity, only run this test on hosts that we can predict. | ||
Target t = get_host_target(); | ||
if (t.arch != Target::X86 || t.bits != 64 || t.os != Target::OSX) { | ||
printf("[SKIP] This test only runs on x86-64-osx.\n"); | ||
return 0; | ||
} | ||
|
||
t = t.with_feature(Target::Debug); | ||
|
||
// Full specification round-trip, crazy features | ||
Target t1 = Target(Target::OSX, Target::X86, 64, | ||
{Target::CUDA, Target::Debug}); | ||
|
||
Expr is_arm = target_arch_is(Target::ARM); | ||
Expr is_x86 = target_arch_is(Target::X86); | ||
Expr bits = target_bits(); | ||
Expr is_android = target_os_is(Target::Android); | ||
Expr is_osx = target_os_is(Target::OSX); | ||
Expr vec = target_natural_vector_size<float>(); | ||
Expr has_cuda = target_has_feature(Target::CUDA); | ||
Expr has_vulkan = target_has_feature(Target::Vulkan); | ||
|
||
Func f; | ||
Var x; | ||
|
||
f(x) = select(is_arm, 1, 0) + | ||
select(is_x86, 2, 0) + | ||
select(vec == 4, 4, 0) + | ||
select(is_android, 8, 0) + | ||
select(is_osx, 16, 0) + | ||
select(bits == 32, 32, 0) + | ||
select(bits == 64, 64, 0) + | ||
select(has_cuda, 128, 0) + | ||
select(has_vulkan, 256, 0); | ||
|
||
Buffer<int> result = f.realize({1}, t1); | ||
|
||
assert(result(0) == 2 + 4 + 16 + 64 + 128); | ||
|
||
printf("Success!\n"); | ||
return 0; | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one should perhaps get a big warning that this does not check runtime-features of the CPU or device, but uses the compile-time target. I know it says that in the doc above, but I feel like many people will get fooled by their wrong intuition. It's especially tricky, as it returns an
Expr
and not abool
, so it gives off a runtime-vibe.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done