-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper functions to query properties of the lowered Target (#8192)
- Loading branch information
1 parent
5d1472f
commit 122ff30
Showing
11 changed files
with
207 additions
and
0 deletions.
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
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; | ||
} |