Skip to content

Commit

Permalink
Merge pull request #5026 from halide/srj-glsl
Browse files Browse the repository at this point in the history
Combine visit(Cast) for GLSL and OpenGLCompute
  • Loading branch information
steven-johnson authored Jun 11, 2020
2 parents 3721fcb + ab1c53e commit 3d19643
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 42 deletions.
27 changes: 0 additions & 27 deletions src/CodeGen_OpenGLCompute_Dev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,33 +76,6 @@ int thread_loop_workgroup_index(const string &name) {
}
} // namespace

void CodeGen_OpenGLCompute_Dev::CodeGen_OpenGLCompute_C::visit(const Cast *op) {
Type value_type = op->value.type();
// If both types are represented by the same GLSL type, no explicit cast
// is necessary.
if (map_type(op->type) == map_type(value_type)) {
Expr value = op->value;
if (value_type.code() == Type::Float) {
// float->int conversions may need explicit truncation if an
// integer type is embedded into a float. (Note: overflows are
// considered undefined behavior, so we do nothing about values
// that are out of range of the target type.)
if (op->type.code() == Type::UInt) {
value = simplify(floor(value));
} else if (op->type.code() == Type::Int) {
value = simplify(trunc(value));
}
}
// FIXME: Overflow is not UB for most Halide types
// https://github.com/halide/Halide/issues/4975
value.accept(this);
return;
} else {
Type target_type = map_type(op->type);
print_assignment(target_type, print_type(target_type) + "(" + print_expr(op->value) + ")");
}
}

void CodeGen_OpenGLCompute_Dev::CodeGen_OpenGLCompute_C::visit(const Call *op) {
if (op->is_intrinsic(Call::gpu_thread_barrier)) {
stream << get_indent() << "barrier();\n";
Expand Down
3 changes: 1 addition & 2 deletions src/CodeGen_OpenGLCompute_Dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,12 @@ class CodeGen_OpenGLCompute_Dev : public CodeGen_GPU_Dev {
protected:
std::string print_type(Type type, AppendSpaceIfNeeded space_option = DoNotAppendSpace) override;

using CodeGen_C::visit;
using CodeGen_GLSLBase::visit;
void visit(const For *) override;
void visit(const Ramp *op) override;
void visit(const Broadcast *op) override;
void visit(const Load *op) override;
void visit(const Store *op) override;
void visit(const Cast *op) override;
void visit(const Call *op) override;
void visit(const Allocate *op) override;
void visit(const Free *op) override;
Expand Down
23 changes: 12 additions & 11 deletions src/CodeGen_OpenGL_Dev.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,16 +426,7 @@ string CodeGen_GLSLBase::print_name(const string &name) {
return replace_all(mangled, "__", "XX");
}

//
// CodeGen_GLSL
//

CodeGen_GLSL::CodeGen_GLSL(std::ostream &s, const Target &t)
: CodeGen_GLSLBase(s, t) {
builtin["trunc_f32"] = "_trunc_f32";
}

void CodeGen_GLSL::visit(const Cast *op) {
void CodeGen_GLSLBase::visit(const Cast *op) {
Type value_type = op->value.type();
// If both types are represented by the same GLSL type, no explicit cast
// is necessary.
Expand All @@ -452,14 +443,24 @@ void CodeGen_GLSL::visit(const Cast *op) {
value = simplify(trunc(value));
}
}
// FIXME: Overflow is not UB for most Halide types
// https://github.com/halide/Halide/issues/4975
value.accept(this);
return;
} else {
Type target_type = map_type(op->type);
print_assignment(target_type, print_type(target_type) + "(" + print_expr(op->value) + ")");
}
}

//
// CodeGen_GLSL
//

CodeGen_GLSL::CodeGen_GLSL(std::ostream &s, const Target &t)
: CodeGen_GLSLBase(s, t) {
builtin["trunc_f32"] = "_trunc_f32";
}

void CodeGen_GLSL::visit(const Let *op) {

if (op->name.find(".varying") != string::npos) {
Expand Down
6 changes: 4 additions & 2 deletions src/CodeGen_OpenGL_Dev.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class CodeGen_GLSLBase : public CodeGen_C {

protected:
using CodeGen_C::visit;

void visit(const Cast *) override;

void visit(const FloatImm *) override;
void visit(const UIntImm *) override;
void visit(const IntImm *) override;
Expand Down Expand Up @@ -99,9 +102,8 @@ class CodeGen_GLSL : public CodeGen_GLSLBase {
static void test();

protected:
using CodeGen_C::visit;
using CodeGen_GLSLBase::visit;

void visit(const Cast *) override;
void visit(const Let *) override;
void visit(const For *) override;
void visit(const Select *) override;
Expand Down

0 comments on commit 3d19643

Please sign in to comment.