Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
43 changes: 17 additions & 26 deletions compiler/rustc_builtin_macros/src/alloc_error_handler.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use rustc_ast::expand::allocator::{ALLOC_ERROR_HANDLER, global_fn_name};
use rustc_ast::{
self as ast, Fn, FnHeader, FnSig, Generics, ItemKind, Safety, Stmt, StmtKind, TyKind,
};
use rustc_ast::{self as ast, FnHeader, FnSig, Generics, ItemKind, Safety, Stmt, StmtKind, TyKind};
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::{Ident, Span, kw, sym};
use thin_vec::{ThinVec, thin_vec};
use rustc_span::{Ident, Span, sym};
use thin_vec::thin_vec;

use crate::diagnostics;
use crate::util::check_builtin_macro_attribute;
use crate::util::{check_builtin_macro_attribute, path_std};

pub(crate) fn expand(
ecx: &mut ExtCtxt<'_>,
Expand Down Expand Up @@ -42,10 +40,7 @@ pub(crate) fn expand(
let stmts = thin_vec![generate_handler(ecx, ident, span, sig_span)];

// Generate anonymous constant serving as container for the allocator methods.
let const_ty = ecx.ty(sig_span, TyKind::Tup(ThinVec::new()));
let const_body = ecx.expr_block(ecx.block(span, stmts));
let const_item =
ecx.item_const(span, Ident::new(kw::Underscore, span), const_ty, Some(const_body));
let const_item = ecx.item_const_underscore(span, ecx.block(span, stmts));
let const_item = if is_stmt {
Annotatable::Stmt(Box::new(ecx.stmt_item(span, const_item)))
} else {
Expand All @@ -61,38 +56,34 @@ pub(crate) fn expand(
// handler(core::alloc::Layout::from_size_align_unchecked(size, align))
// }
fn generate_handler(cx: &ExtCtxt<'_>, handler: Ident, span: Span, sig_span: Span) -> Stmt {
let usize = cx.path_ident(span, Ident::new(sym::usize, span));
let ty_usize = cx.ty_path(usize);
let size = Ident::new(sym::size, span);
let align = Ident::new(sym::align, span);
let ty_usize = cx.ty_sym(span, sym::usize);

let layout_new = cx.std_path(&[sym::alloc, sym::Layout, sym::from_size_align_unchecked]);
let layout_new = cx.expr_path(cx.path(span, layout_new));
let layout_new = path_std!(cx, span, alloc::Layout::from_size_align_unchecked);
let layout_new = cx.expr_path(layout_new);
let layout = cx.expr_call(
span,
layout_new,
thin_vec![cx.expr_ident(span, size), cx.expr_ident(span, align)],
thin_vec![cx.expr_ident_sym(span, sym::size), cx.expr_ident_sym(span, sym::align)],
);

let call = cx.expr_call_ident(sig_span, handler, thin_vec![layout]);

let never = ast::FnRetTy::Ty(cx.ty(span, TyKind::Never));
let params = thin_vec![cx.param(span, size, ty_usize.clone()), cx.param(span, align, ty_usize)];
let params = thin_vec![
cx.param(span, sym::size, ty_usize.clone()),
cx.param(span, sym::align, ty_usize)
];
let decl = cx.fn_decl(params, never);
let header = FnHeader { safety: Safety::Unsafe(span), ..FnHeader::default() };
let sig = FnSig { decl, header, span };

let body = Some(cx.block_expr(call));
let kind = ItemKind::Fn(Box::new(Fn {
defaultness: ast::Defaultness::Implicit,
let kind = ItemKind::Fn(cx.item_fn(
sig,
ident: Ident::from_str_and_span(&global_fn_name(ALLOC_ERROR_HANDLER), span),
generics: Generics::default(),
contract: None,
Ident::from_str_and_span(&global_fn_name(ALLOC_ERROR_HANDLER), span),
Generics::default(),
body,
define_opaque: None,
eii_impl: None,
}));
));

let attrs = thin_vec![cx.attr_word(sym::rustc_std_internal_symbol, span)];

Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_builtin_macros/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use thin_vec::thin_vec;

use crate::diagnostics;
use crate::edition_panic::use_panic_2021;
use crate::util::path_std;

pub(crate) fn expand_assert<'cx>(
cx: &'cx mut ExtCtxt<'_>,
Expand All @@ -34,7 +35,7 @@ pub(crate) fn expand_assert<'cx>(
let panic_path = || {
if use_panic_2021(span) {
// On edition 2021, we always call `$crate::panic::panic_2021!()`.
cx.path(call_site_span, cx.std_path(&[sym::panic, sym::panic_2021]))
path_std!(cx, call_site_span, panic::panic_2021)
} else {
// Before edition 2021, we call `panic!()` unqualified,
// such that it calls either `std::panic!()` or `core::panic!()`.
Expand Down Expand Up @@ -64,7 +65,7 @@ pub(crate) fn expand_assert<'cx>(
// passed literally.
let then = cx.expr_call_global(
call_site_span,
cx.std_path(&[sym::panicking, sym::panic]),
path_std!(cx, call_site_span, panicking::panic),
thin_vec![cx.expr_str(
DUMMY_SP,
Symbol::intern(&format!(
Expand Down
31 changes: 13 additions & 18 deletions compiler/rustc_builtin_macros/src/assert/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use rustc_expand::base::ExtCtxt;
use rustc_span::{Ident, Span, Symbol, sym};
use thin_vec::{ThinVec, thin_vec};

use crate::util::path_std;

pub(super) struct Context<'cx, 'a> {
// An optimization.
//
Expand Down Expand Up @@ -99,7 +101,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
fn build_initial_imports(&self) -> Stmt {
let nested_tree = |this: &Self, sym| UseTreeAndId {
inner: UseTree {
prefix: this.cx.path(this.span, vec![Ident::with_dummy_span(sym)]),
prefix: this.cx.path_sym(this.span, sym),
kind: UseTreeKind::Simple(None),
},
id: DUMMY_NODE_ID,
Expand All @@ -110,7 +112,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
self.span,
thin_vec![self.cx.attr_nested_word(sym::allow, sym::unused_imports, self.span)],
ItemKind::Use(UseTree {
prefix: self.cx.path(self.span, self.cx.std_path(&[sym::asserting])),
prefix: path_std!(self.cx, self.span, asserting),
kind: UseTreeKind::Nested {
items: thin_vec![
nested_tree(self, sym::TryCaptureGeneric),
Expand All @@ -125,10 +127,9 @@ impl<'cx, 'a> Context<'cx, 'a> {

/// Takes the conditional expression of `assert!` and then wraps it inside `unlikely`
fn build_unlikely(&self, cond_expr: Box<Expr>) -> Box<Expr> {
let unlikely_path = self.cx.std_path(&[sym::intrinsics, sym::unlikely]);
self.cx.expr_call(
self.cx.expr_call_intrinsic(
self.span,
self.cx.expr_path(self.cx.path(self.span, unlikely_path)),
sym::unlikely,
thin_vec![self.cx.expr(self.span, ExprKind::Unary(UnOp::Not, cond_expr))],
)
}
Expand Down Expand Up @@ -342,13 +343,9 @@ impl<'cx, 'a> Context<'cx, 'a> {
let curr_capture_idx = self.capture_decls.len();
let capture_string = format!("__capture{curr_capture_idx}");
let ident = Ident::new(Symbol::intern(&capture_string), self.span);
let init_std_path = self.cx.std_path(&[sym::asserting, sym::Capture, sym::new]);
let init = self.cx.expr_call(
self.span,
self.cx.expr_path(self.cx.path(self.span, init_std_path)),
ThinVec::new(),
);
let capture = Capture { decl: self.cx.stmt_let(self.span, true, ident, init), ident };
let init_std_path = path_std!(self.cx, self.span, asserting::Capture::new);
let init = self.cx.expr_call(self.span, self.cx.expr_path(init_std_path), ThinVec::new());
let capture = Capture { decl: self.cx.stmt_let(self.span, true, ident.name, init), ident };
self.capture_decls.push(capture);
self.manage_try_capture(ident, curr_capture_idx, expr);
}
Expand All @@ -366,7 +363,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
expr: &mut Box<Expr>,
) {
let local_bind_string = format!("__local_bind{curr_capture_idx}");
let local_bind = Ident::new(Symbol::intern(&local_bind_string), self.span);
let local_bind = Symbol::intern(&local_bind_string);
self.local_bind_decls.push(self.cx.stmt_let(
self.span,
false,
Expand All @@ -375,10 +372,8 @@ impl<'cx, 'a> Context<'cx, 'a> {
));
let wrapper = self.cx.expr_call(
self.span,
self.cx.expr_path(
self.cx.path(self.span, self.cx.std_path(&[sym::asserting, sym::Wrapper])),
),
thin_vec![self.cx.expr_path(Path::from_ident(local_bind))],
self.cx.expr_path(path_std!(self.cx, self.span, asserting::Wrapper)),
thin_vec![self.cx.expr_path(self.cx.path_sym(self.span, local_bind))],
);
let try_capture_call = self
.cx
Expand All @@ -393,7 +388,7 @@ impl<'cx, 'a> Context<'cx, 'a> {
)],
))
.add_trailing_semicolon();
let local_bind_path = self.cx.expr_path(Path::from_ident(local_bind));
let local_bind_path = self.cx.expr_path(self.cx.path_sym(self.span, local_bind));
let rslt = if self.is_consumed {
let ret = self.cx.stmt_expr(local_bind_path);
self.cx.expr_block(self.cx.block(self.span, thin_vec![try_capture_call, ret]))
Expand Down
42 changes: 12 additions & 30 deletions compiler/rustc_builtin_macros/src/autodiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod llvm_enzyme {
use rustc_ast::tokenstream::*;
use rustc_ast::visit::AssocCtxt::*;
use rustc_ast::{
self as ast, AnonConst, FnRetTy, FnSig, GenericArg, GenericParamKind, Generics, ItemKind,
self as ast, FnRetTy, FnSig, GenericArg, GenericParamKind, Generics, ItemKind,
MetaItemInner, PatKind, TyKind, Visibility,
};
use rustc_attr_ir::RustcAutodiff;
Expand Down Expand Up @@ -332,16 +332,7 @@ mod llvm_enzyme {
);

// The first element of it is the name of the function to be generated
let d_fn = Box::new(ast::Fn {
defaultness: ast::Defaultness::Implicit,
sig: d_sig,
ident: first_ident(&meta_item_vec[0]),
generics,
contract: None,
body: Some(d_body),
define_opaque: None,
eii_impl: None,
});
let d_fn = ecx.item_fn(d_sig, first_ident(&meta_item_vec[0]), generics, Some(d_body));
let mut rustc_ad_attr =
Box::new(ast::NormalAttr::from_ident(Ident::with_dummy_span(sym::rustc_autodiff)));

Expand Down Expand Up @@ -502,7 +493,7 @@ mod llvm_enzyme {
) -> rustc_ast::Stmt {
let primal_path_expr = gen_turbofish_expr(ecx, primal, generics, span, is_impl);

let self_ty = || ecx.ty_path(ast::Path::from_ident(Ident::with_dummy_span(kw::SelfUpper)));
let self_ty = || ecx.ty_self(DUMMY_SP);
let fn_ptr_params: ThinVec<ast::Param> = p_sig
.decl
.inputs
Expand Down Expand Up @@ -562,11 +553,9 @@ mod llvm_enzyme {
.collect::<ThinVec<_>>(),
);

let enzyme_path_idents = ecx.std_path(&[sym::intrinsics, sym::autodiff]);
let enzyme_path = ecx.path(span, enzyme_path_idents);
let call_expr = ecx.expr_call(
let call_expr = ecx.expr_call_intrinsic(
span,
ecx.expr_path(enzyme_path),
sym::autodiff,
thin_vec![primal_fn_ptr, diff_path_expr, tuple_expr],
);

Expand All @@ -593,17 +582,15 @@ mod llvm_enzyme {
Some(GenericArg::Type(ty))
}
GenericParamKind::Const { .. } => {
let expr = ecx.expr_path(ast::Path::from_ident(p.ident));
let anon_const = AnonConst { id: ast::DUMMY_NODE_ID, value: expr };
let anon_const = ecx.const_ident(span, p.ident);
Some(GenericArg::Const(anon_const))
}
GenericParamKind::Lifetime => None,
})
.collect::<Vec<_>>();

let idents =
if is_impl { vec![Ident::new(kw::SelfUpper, span), ident] } else { vec![ident] };
let path = ecx.path_all(span, false, idents, generic_args);
let idents = if is_impl { &[Ident::new(kw::SelfUpper, span), ident][..] } else { &[ident] };
let path = ecx.path_all(span, idents, generic_args);

ecx.expr_path(path)
}
Expand Down Expand Up @@ -707,8 +694,7 @@ mod llvm_enzyme {
panic!("not an ident?");
};
let name = format!("d{}_{}", old_name, i);
let ident = Ident::from_str_and_span(&name, shadow_arg.pat.span);
*shadow_arg.pat = ecx.pat_ident(shadow_arg.pat.span, ident);
*shadow_arg.pat = ecx.pat_ident(shadow_arg.pat.span, Symbol::intern(&name));
d_inputs.push(shadow_arg);
}
}
Expand All @@ -733,9 +719,8 @@ mod llvm_enzyme {
panic!("not an ident?");
};
let name = format!("b{}_{}", old_name, i);
let ident = Ident::from_str_and_span(&name, shadow_arg.pat.span);

*shadow_arg.pat = ecx.pat_ident(shadow_arg.pat.span, ident);
*shadow_arg.pat = ecx.pat_ident(shadow_arg.pat.span, Symbol::intern(&name));
d_inputs.push(shadow_arg);
}
}
Expand Down Expand Up @@ -764,9 +749,7 @@ mod llvm_enzyme {
panic!("Did not expect Default ret ty: {:?}", span);
}
};
let name = "dret".to_string();
let ident = Ident::from_str_and_span(&name, ty.span);
let shadow_arg = ecx.param(ty.span, ident, ty);
let shadow_arg = ecx.param(ty.span, sym::dret, ty);
d_inputs.push(shadow_arg);
}
_ => {}
Expand Down Expand Up @@ -841,8 +824,7 @@ mod llvm_enzyme {
if act_ret.len() == 1 {
act_ret[0].clone()
} else {
let kind = TyKind::Tup(act_ret);
Box::new(rustc_ast::Ty { kind, id: ast::DUMMY_NODE_ID, span })
ecx.ty(span, TyKind::Tup(act_ret))
}
}
};
Expand Down
Loading
Loading