Skip to content
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 more attributes to highlevel dialect #594

Merged
merged 22 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c3f6eeb
hl: Add Leaf and Cold attributes.
Jezurko May 28, 2024
564df3e
hl: Add template class for attributes holding a number.
Jezurko May 29, 2024
179415d
hl: Add hollow aligned attribute implementation.
Jezurko May 29, 2024
1935cc5
cg: Add visitor for implicit value init expr.
Jezurko May 29, 2024
a6e0f75
hl: Add Deprecated attribute.
Jezurko May 29, 2024
2895363
cg: Add Deprecated attribute visitor.
Jezurko May 29, 2024
1851e88
test: Add test for deprecated attribute.
Jezurko May 29, 2024
2c0b245
hl: Add transparent union attribute.
Jezurko May 29, 2024
76937a3
cg: Add transparent union attribute visitor.
Jezurko May 29, 2024
605c446
hl: Add compound literal op.
Jezurko May 29, 2024
b92744a
cg: Add compound literal expression visitor.
Jezurko May 29, 2024
646ccb4
test: Add test for transparent_union.
Jezurko May 29, 2024
803bfde
hl: Add returns twice attribute.
Jezurko May 30, 2024
d265efa
cg: Add returns twice attribute visitor.
Jezurko May 30, 2024
fca82e7
cg: Add helper for visiting type of literal expression with possibly …
Jezurko May 30, 2024
e8b4dfd
cg: Fix CompoundLiteral not having lvalue in some cases.
Jezurko May 30, 2024
4296304
test: Add test for compound literal lvalue.
Jezurko May 30, 2024
e87efff
cg: Imporve comment for for literal lvalue type helper.
Jezurko May 30, 2024
8111889
hl: Remove unused number attribute helper.
Jezurko May 30, 2024
02c69f2
hl: Cleanup DeprecatedAttr tablegen.
Jezurko May 30, 2024
831a7f2
hl: Add description strings to CompoundLiteralOp.
Jezurko May 30, 2024
5c15857
hl: Clean up tablegen code style.
xlauko May 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion include/vast/CodeGen/DefaultAttrVisitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace vast::cg {
mlir_attr VisitSectionAttr(const clang::SectionAttr *attr);
mlir_attr VisitFormatAttr(const clang::FormatAttr *attr);
mlir_attr VisitAnnotateAttr(const clang::AnnotateAttr *attr);
mlir_attr VisitAlignedAttr(const clang::AlignedAttr *attr);
mlir_attr VisitAlwaysInlineAttr(const clang::AlwaysInlineAttr *attr);
mlir_attr VisitLoaderUninitializedAttr(const clang::LoaderUninitializedAttr *attr);
mlir_attr VisitNoInstrumentFunctionAttr(const clang::NoInstrumentFunctionAttr *attr);
Expand All @@ -39,6 +40,11 @@ namespace vast::cg {
mlir_attr VisitAsmLabelAttr(const clang::AsmLabelAttr *attr);
mlir_attr VisitAllocAlignAttr(const clang::AllocAlignAttr *attr);
mlir_attr VisitAllocSizeAttr(const clang::AllocSizeAttr *attr);
mlir_attr VisitLeafAttr(const clang::LeafAttr *attr);
mlir_attr VisitColdAttr(const clang::ColdAttr *attr);
mlir_attr VisitDeprecatedAttr(const clang::DeprecatedAttr *attr);
mlir_attr VisitTransparentUnionAttr(const clang::TransparentUnionAttr *attr);
mlir_attr VisitReturnsTwiceAttr(const clang::ReturnsTwiceAttr *attr);

private:
template< typename attr_t, typename... args_t >
Expand All @@ -47,4 +53,4 @@ namespace vast::cg {
}
};

} // namespace vast::cg
} // namespace vast::cg
8 changes: 8 additions & 0 deletions include/vast/CodeGen/DefaultStmtVisitor.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022-present, Trail of Bits, Inc.

Check notice on line 1 in include/vast/CodeGen/DefaultStmtVisitor.hpp

View workflow job for this annotation

GitHub Actions / cpp-linter (18, 22.04)

Run clang-format on include/vast/CodeGen/DefaultStmtVisitor.hpp

File include/vast/CodeGen/DefaultStmtVisitor.hpp does not conform to Custom style guidelines. (lines 259)

#pragma once

Expand Down Expand Up @@ -251,6 +251,13 @@
//
// Literals
//

// Helper for use with literal expressions that might return lvalue.
// It takes an expression, checks if it's lvalue expression
// and depending on the result calls either the lvalue type visitor or
// the standard type visitor.
mlir_type visit_maybe_lvalue_literal_type (const clang::Expr *expr);

operation VisitCharacterLiteral(const clang::CharacterLiteral *lit);
operation VisitIntegerLiteral(const clang::IntegerLiteral *lit);
operation VisitFloatingLiteral(const clang::FloatingLiteral *lit);
Expand All @@ -261,6 +268,7 @@
operation VisitImaginaryLiteral(const clang::ImaginaryLiteral *lit);

operation VisitInitListExpr(const clang::InitListExpr *expr);
operation VisitImplicitValueInitExpr(const clang::ImplicitValueInitExpr *expr);
};

template< typename Op >
Expand Down
29 changes: 27 additions & 2 deletions include/vast/Dialect/HighLevel/HighLevelAttributes.td
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class NameAttr<string name, string attr_mnemonic>
: HighLevel_Attr< name, attr_mnemonic >
{
let parameters = (ins "::mlir::StringAttr":$name);

let builders = [
AttrBuilderWithInferredContext<(ins "::mlir::StringAttr":$name), [{
return get(name.getContext(), name);
Expand All @@ -33,6 +34,8 @@ class NameAttr<string name, string attr_mnemonic>
def AnnotationAttr : NameAttr< "Annotation", "annotation" >;
def FormatAttr : NameAttr< "Format", "format" >;
def SectionAttr : NameAttr< "Section", "section" >;
// TODO(#595): Make aligned attribute keep the alignment value/expr
def AlignedAttr : HighLevel_Attr< "Aligned", "aligned" >;
def AlwaysInlineAttr : HighLevel_Attr< "AlwaysInline", "always_inline" >;
def ConstAttr : HighLevel_Attr< "Const", "const" >;
def LoaderUninitAttr : HighLevel_Attr< "LoaderUninitialized", "loader_uninitialized" >;
Expand All @@ -43,9 +46,14 @@ def WarnUnusedResAttr : HighLevel_Attr< "WarnUnusedResult", "warn_unused_result"
def RestrictAttr : HighLevel_Attr< "Restrict", "restrict" >;
def NoThrowAttr : HighLevel_Attr< "NoThrow", "nothrow" >;
def NonNullAttr : HighLevel_Attr< "NonNull", "nonnull" >;
def LeafAttr : HighLevel_Attr< "Leaf", "leaf" >;
def ColdAttr : HighLevel_Attr< "Cold", "cold" >;
def TransparentUnionAttr : HighLevel_Attr< "TransparentUnion", "transparent_union" >;
def ReturnsTwiceAttr : HighLevel_Attr< "ReturnsTwice", "returns_twice" >;

def AsmLabelAttr : HighLevel_Attr< "AsmLabel", "asm" > {
let parameters = (ins "::mlir::StringAttr":$label, "bool":$isLiteral);

let builders = [
AttrBuilderWithInferredContext<(ins "::mlir::StringAttr":$label, "bool":$literal), [{
return get(label.getContext(), label, literal);
Expand All @@ -60,6 +68,7 @@ def AsmLabelAttr : HighLevel_Attr< "AsmLabel", "asm" > {

def ModeAttr : HighLevel_Attr< "Mode", "mode" > {
let parameters = (ins "::mlir::StringAttr":$mode);

let builders = [
AttrBuilderWithInferredContext<(ins "::mlir::StringAttr":$mode), [{
return get(mode.getContext(), mode);
Expand All @@ -74,19 +83,35 @@ def ModeAttr : HighLevel_Attr< "Mode", "mode" > {

def BuiltinAttr : HighLevel_Attr< "Builtin", "builtin" > {
let parameters = (ins "unsigned":$ID);

let assemblyFormat = "`<` $ID `>`";
}

def AllocAlignAttr : HighLevel_Attr< "AllocAlign", "alloc_align" >{
def AllocAlignAttr : HighLevel_Attr< "AllocAlign", "alloc_align" > {
let parameters = (ins "int":$alignment);

let assemblyFormat = "`<` $alignment `>`";
}

def AllocSizeAttr : HighLevel_Attr< "AllocSize", "alloc_size" >{
def AllocSizeAttr : HighLevel_Attr< "AllocSize", "alloc_size" > {
let parameters = (ins "int":$size_arg_pos, OptionalParameter< "int" >:$num_arg_pos);

let assemblyFormat = "`<` `size_pos` `:` $size_arg_pos (`,` `num_pos` `:` $num_arg_pos^)? `>`";
}

def DeprecatedAttr : HighLevel_Attr< "Deprecated", "deprecated" > {
let parameters = (ins
"::mlir::StringAttr":$message,
"::mlir::StringAttr":$fixit
);

let builders = [
AttrBuilder<(ins "::mlir::StringRef":$msg, "::mlir::StringRef":$fix), [{
return get($_ctxt, mlir::StringAttr::get($_ctxt, msg), mlir::StringAttr::get($_ctxt, fix));
}]>,
];

let assemblyFormat = "`<` `msg` `:` $message `,` `fix` `:` $fixit `>`";
}

#endif // VAST_DIALECT_HIGHLEVEL_IR_HIGHLEVELATTRIBUTES
63 changes: 49 additions & 14 deletions include/vast/Dialect/HighLevel/HighLevelOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -468,16 +468,23 @@ def ConstantOp
def InitializedConstantOp : HighLevel_Op< "const.init", [ConstantLike, Pure] >
, Results< (outs AnyType:$result) >
{
let summary = "VAST value constant";
let description = [{ VAST value constant witch requires region initialization }];
let summary = "VAST constant with non-trivial initialization";
let description = [{
InitializedConstantOp is used to represent a constant with non-trivial
initialization. The initialization is represented by a region that
represents initialization expression.
}];

let regions = (region ValueRegion:$init);

let skipDefaultBuilders = 1;
let builders = [
OpBuilder<(ins
"Type":$type,
"builder_callback":$initbuilder
)>
];

let assemblyFormat = [{ attr-dict `:` type($result) `=` $init }];
}

Expand All @@ -487,6 +494,30 @@ def UnreachableOp : HighLevel_Op<"unreachable", [Terminator]> {
let assemblyFormat = "attr-dict";
}

def CompoundLiteralOp : HighLevel_Op< "compound_literal" >
, Results< (outs AnyType:$result) >
{
let summary = "VAST compound literals operation";
let description = [{
Creates a compound literal that represents an unnamed object and is
initialzied by initializer-list.

Is used to represetnt `clang::CompoundLiteralExpr`.
}];

let regions = (region ValueRegion:$init);

let skipDefaultBuilders = 1;
let builders = [
OpBuilder<(ins
"Type":$type,
"builder_callback":$initbuilder
)>
];

let assemblyFormat = [{ attr-dict `:` type($result) `=` $init }];
}

class CastKindAttr< string name, int val > : I64EnumAttrCase< name, val > {}

class CastKindList< string name, string summary, list< CastKindAttr > cases >
Expand Down Expand Up @@ -745,12 +776,12 @@ class StandardArithBinOp< string mnemonic, list< Trait > traits = [] >
class FloatArithBinOp< string mnemonic, list< Trait > traits = [] >
: ArithBinOp< mnemonic, traits >
{
let hasVerifier = 1;
let extraClassDefinition = [{
let hasVerifier = 1;
let extraClassDefinition = [{
logical_result $cppClass::verify() {
return verify_float_arith_op(this->getOperation());
return verify_float_arith_op(this->getOperation());
}
}];
}];
}

def AddIOp : ArithBinOp< "add", [Commutative, IsAdditive< "lhs", "rhs", "result" >] >;
Expand Down Expand Up @@ -1030,6 +1061,7 @@ def FCmpOp
{
let summary = "VAST flaoting point comparison operation";
let description = [{ VAST floating point comparison operation }];

let hasVerifier = 1;

let assemblyFormat = "$predicate $lhs `,` $rhs attr-dict `:` type(operands) `->` type($result)";
Expand Down Expand Up @@ -1270,8 +1302,8 @@ def IdentKind : IdentKindList< "IdentKind", "ident kind", [

def PredefinedExpr
: HighLevel_Op< "predefined.expr" >
, Arguments<(ins AnyType:$value, IdentKind:$kind)>
, Results<(outs AnyType:$result)>
, Arguments<(ins AnyType:$value, IdentKind:$kind)>
, Results<(outs AnyType:$result)>
{
let summary = "VAT predefined expr ( such as __func__ )";
let description = [{ VAT predefined expr ( such as __func__ ) }];
Expand All @@ -1281,8 +1313,8 @@ def PredefinedExpr

def ExtensionOp
: HighLevel_Op< "gnu.extension" >
, Arguments<(ins AnyType:$value)>
, Results<(outs AnyType:$result)>
, Arguments<(ins AnyType:$value)>
, Results<(outs AnyType:$result)>
{
let summary = "VAST extension (__extension__) keyword";
let description = [{ VAST op corresponding to GNU __extension__ keyword. }];
Expand Down Expand Up @@ -1314,11 +1346,12 @@ def AsmOp
)>
{
let summary = "VAST operation for inline assembly";
let description = [{ VAST operation mirroring the GCCAsmStmt in clang AST. It prints
a name for every operand (either its id or user-supplied string).}];
let description = [{
VAST operation mirroring the GCCAsmStmt in clang AST. It prints a name for
every operand (either its id or user-supplied string).
}];

let skipDefaultBuilders = 1;

let builders = [
OpBuilder< (ins
"mlir::StringAttr":$asm_template,
Expand All @@ -1335,7 +1368,9 @@ def AsmOp
) >
];

let assemblyFormat = [{attr-dict $asm_template `(`($output_names $asm_outputs^ `:` $output_constraints)? `)` `(` (`ins` `:`$input_names $asm_inputs^ `:` $input_constraints)? `)` `(`( $clobbers^)?`)` `(`( $labels^)?`)` `:` functional-type(operands, results)}];
let assemblyFormat = [{
attr-dict $asm_template `(`($output_names $asm_outputs^ `:` $output_constraints)? `)` `(` (`ins` `:`$input_names $asm_inputs^ `:` $input_constraints)? `)` `(`( $clobbers^)?`)` `(`( $labels^)?`)` `:` functional-type(operands, results)
}];
}

def VAArgExpr
Expand Down
25 changes: 25 additions & 0 deletions lib/vast/CodeGen/DefaultAttrVisitor.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024, Trail of Bits, Inc.

Check notice on line 1 in lib/vast/CodeGen/DefaultAttrVisitor.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter (18, 22.04)

Run clang-format on lib/vast/CodeGen/DefaultAttrVisitor.cpp

File lib/vast/CodeGen/DefaultAttrVisitor.cpp does not conform to Custom style guidelines. (lines 99)

#include "vast/CodeGen/DefaultAttrVisitor.hpp"

Expand All @@ -22,6 +22,11 @@
return make< hl::AnnotationAttr >(attr->getAnnotation());
}

mlir_attr default_attr_visitor::VisitAlignedAttr(const clang::AlignedAttr *attr) {
// TODO(#595): Keep the alignment in the attribute
return make< hl::AlignedAttr >();
}

mlir_attr default_attr_visitor::VisitAlwaysInlineAttr(const clang::AlwaysInlineAttr *) {
return make< hl::AlwaysInlineAttr >();
}
Expand Down Expand Up @@ -78,4 +83,24 @@
auto num = attr->getNumElemsParam().isValid() ? attr->getNumElemsParam().getSourceIndex() : int();
return make< hl::AllocSizeAttr >(attr->getElemSizeParam().getSourceIndex(), num);
}

mlir_attr default_attr_visitor::VisitLeafAttr(const clang::LeafAttr *attr) {
return make< hl::LeafAttr >();
}

mlir_attr default_attr_visitor::VisitColdAttr(const clang::ColdAttr *attr) {
return make< hl::ColdAttr >();
}

mlir_attr default_attr_visitor::VisitDeprecatedAttr(const clang::DeprecatedAttr *attr) {
return make< hl::DeprecatedAttr >(attr->getMessage(), attr->getReplacement());
}

mlir_attr default_attr_visitor::VisitTransparentUnionAttr(const clang::TransparentUnionAttr *attr) {
return make< hl::TransparentUnionAttr >();
}

mlir_attr default_attr_visitor::VisitReturnsTwiceAttr(const clang::ReturnsTwiceAttr *attr) {
return make< hl::ReturnsTwiceAttr >();
}
} // namespace vast::hcg
34 changes: 23 additions & 11 deletions lib/vast/CodeGen/DefaultStmtVisitor.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022-present, Trail of Bits, Inc.

Check notice on line 1 in lib/vast/CodeGen/DefaultStmtVisitor.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter (18, 22.04)

Run clang-format on lib/vast/CodeGen/DefaultStmtVisitor.cpp

File lib/vast/CodeGen/DefaultStmtVisitor.cpp does not conform to Custom style guidelines. (lines 565, 566, 567, 584, 585, 586, 593, 1016)

#include "vast/CodeGen/DefaultStmtVisitor.hpp"

Expand Down Expand Up @@ -562,6 +562,12 @@
//
// Literals
//
mlir_type default_stmt_visitor::visit_maybe_lvalue_literal_type (const clang::Expr *lit){
if (lit->isLValue())
return self.visit_as_lvalue_type(lit->getType());
return self.visit(lit->getType());
}

operation default_stmt_visitor::VisitCharacterLiteral(const clang::CharacterLiteral *lit) {
return bld.constant(self.location(lit), self.visit(lit->getType()), apsint(lit->getValue())).getDefiningOp();
}
Expand All @@ -575,23 +581,21 @@
}

operation default_stmt_visitor::VisitStringLiteral(const clang::StringLiteral *lit) {
auto type = [&] {
if (lit->isLValue()) {
return self.visit_as_lvalue_type(lit->getType());
} else {
return self.visit(lit->getType());
}
}();

return bld.constant(self.location(lit), type, lit->getString()).getDefiningOp();
return bld.constant(self.location(lit),
visit_maybe_lvalue_literal_type(lit),
lit->getString()).getDefiningOp();
}

operation default_stmt_visitor::VisitUserDefinedLiteral(const clang::UserDefinedLiteral */* lit */) {
return {};
}

operation default_stmt_visitor::VisitCompoundLiteralExpr(const clang::CompoundLiteralExpr */* lit */) {
return {};
operation default_stmt_visitor::VisitCompoundLiteralExpr(const clang::CompoundLiteralExpr *lit) {
return bld.compose< hl::CompoundLiteralOp >()
.bind(self.location(lit))
.bind(visit_maybe_lvalue_literal_type(lit))
.bind(mk_value_builder(lit->getInitializer()))
.freeze();
}

operation default_stmt_visitor::VisitFixedPointLiteral(const clang::FixedPointLiteral */* lit */) {
Expand Down Expand Up @@ -1009,4 +1013,12 @@
.freeze();
}

operation default_stmt_visitor::VisitImplicitValueInitExpr(const clang::ImplicitValueInitExpr *expr) {
return bld.compose< hl::InitListExpr >()
.bind(self.location(expr))
.bind(self.visit(expr->getType()))
.bind(visit_values_range(expr->children()))
.freeze();
}

} // namespace vast::cg
10 changes: 10 additions & 0 deletions lib/vast/Dialect/HighLevel/HighLevelOps.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-present, Trail of Bits, Inc.

Check notice on line 1 in lib/vast/Dialect/HighLevel/HighLevelOps.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter (18, 22.04)

Run clang-format on lib/vast/Dialect/HighLevel/HighLevelOps.cpp

File lib/vast/Dialect/HighLevel/HighLevelOps.cpp does not conform to Custom style guidelines. (lines 670)

#include "vast/Util/Warnings.hpp"

Expand Down Expand Up @@ -667,6 +667,16 @@
st.addTypes(type);
}

void CompoundLiteralOp::build(Builder &bld, State &st, Type type, builder_callback init)
{
VAST_ASSERT(init && "the builder callback for 'init' region must be present");

InsertionGuard guard(bld);

build_region(bld, st, builder_callback_ref(init));
st.addTypes(type);
}

GRAPH_REGION_OP(FuncOp);
GRAPH_REGION_OP(StmtExprOp);

Expand Down
12 changes: 12 additions & 0 deletions test/vast/Dialect/HighLevel/attr-e.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %vast-front -vast-emit-mlir=hl -o - %s | %file-check %s
// RUN: %vast-front -vast-emit-mlir=hl -o - %s > %t && %vast-opt %t | diff -B %t -

// CHECK: hl.func @fun external () -> !hl.void attributes {deprecated = #hl.deprecated<msg : "msg", fix : "fix">} {
__attribute__((deprecated("msg","fix")))
void fun (void) {};
// CHECK: hl.func @fun1 external () -> !hl.void attributes {deprecated = #hl.deprecated<msg : "msg", fix : "">} {
__attribute__((deprecated("msg")))
void fun1 (void) {};
// CHECK: hl.func @fun2 external () -> !hl.void attributes {deprecated = #hl.deprecated<msg : "", fix : "">} {
__attribute__((deprecated()))
void fun2 (void) {};
Loading