-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnutcracker.sh
More file actions
executable file
·1322 lines (1059 loc) · 40.8 KB
/
Copy pathnutcracker.sh
File metadata and controls
executable file
·1322 lines (1059 loc) · 40.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# ============================================================================
# NutCracker Compiler Build and Run Script
# ============================================================================
set -e # Exit on error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
MAGENTA='\033[0;35m'
BOLD='\033[1m'
NC='\033[0m' # No Color
# Directories
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BUILD_DIR="$ROOT_DIR/build"
APPS_DIR="$ROOT_DIR/apps"
OUTPUT_DIR="$ROOT_DIR/vdsa_output"
P4C_DIR="$ROOT_DIR/third_party/p4mlir/third_party/p4c"
P4_INCLUDE_DIR="$ROOT_DIR/p4include"
# Tools
P4MLIR_TRANSLATE="$P4C_DIR/build/p4mlir-translate"
NUTCRACKER_OPT="$BUILD_DIR/bin/nutcracker-opt"
MAPPER="$ROOT_DIR/deps/mapper/target/release/mapper"
CLANG="${CLANG:-$ROOT_DIR/third_party/llvm/build/bin/clang}"
# ============================================================================
# Helper Functions
# ============================================================================
print_header() {
local title="$1"
echo ""
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo -e "🚀 ${BOLD}${GREEN}$title${NC}"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
echo ""
}
print_section() {
echo ""
echo -e "📦 ${BOLD}${CYAN}$1${NC}"
echo ""
}
print_step() {
echo -e "${GREEN} ▸${NC} $1"
}
print_substep() {
echo -e " ${CYAN}→${NC} $1"
}
print_error() {
echo -e "${RED}❌ Error:${NC} $1" >&2
}
print_success() {
echo -e "${GREEN}✅${NC} $1"
}
print_warning() {
echo -e "${YELLOW}⚠️${NC} $1"
}
print_info() {
echo -e "${BLUE}ℹ️${NC} $1"
}
print_file() {
echo -e " ${MAGENTA}📄${NC} $1"
}
print_dir() {
echo -e " ${BLUE}📁${NC} $1"
}
# ============================================================================
# Setup Functions
# ============================================================================
setup_p4_includes() {
print_header "Setting up P4 Include Directory"
mkdir -p "$P4_INCLUDE_DIR"
# Check if nc.p4 exists
if [ ! -f "$P4_INCLUDE_DIR/nc.p4" ]; then
print_warning "nc.p4 not found in p4include/"
print_step "Creating nc.p4 architecture definition..."
cat > "$P4_INCLUDE_DIR/nc.p4" << 'EOF'
// nc.p4 - NutCracker Architecture Definition
#ifndef _NC_P4_
#define _NC_P4_
#include <core.p4>
// ============================================================================
// Standard Metadata
// ============================================================================
struct nc_standard_metadata_t {
bit<9> ingress_port;
bit<9> egress_port;
bit<32> packet_length;
bit<1> drop;
}
// ============================================================================
// Extern Functions (Fixed-Function Accelerators)
// ============================================================================
extern Counter<I> {
Counter(bit<32> size);
void count(in I index);
}
extern Hash5Tuple {
Hash5Tuple();
void apply(out bit<32>, in bit<32>, in bit<32>,
in bit<16>, in bit<16>, in bit<8>);
}
// ============================================================================
// Programmable Block Type Definitions
// ============================================================================
parser MainParserT<MH, MM>(
packet_in pkt,
out MH main_hdr,
inout MM main_meta,
in nc_standard_metadata_t standard_meta
);
control MainControlT<MH, MM>(
inout MH main_hdr,
inout MM main_meta,
inout nc_standard_metadata_t standard_meta
);
control MainDeparserT<MH, MM>(
packet_out pkt,
in MH main_hdr,
in MM main_meta,
in nc_standard_metadata_t standard_meta
);
// ============================================================================
// NC Pipeline Package
// ============================================================================
package NC_PIPELINE<MH, MM>(
MainParserT<MH, MM> main_parser,
MainControlT<MH, MM> main_control,
MainDeparserT<MH, MM> main_deparser
);
#endif // _NC_P4_
EOF
print_success "Created nc.p4"
else
print_success "nc.p4 already exists"
fi
}
# ============================================================================
# Build Functions
# ============================================================================
build_llvm() {
print_header "Building LLVM"
if [ -d "$ROOT_DIR/third_party/llvm/build" ]; then
print_warning "LLVM already built, skipping..."
return 0
fi
print_step "Running LLVM build script..."
cd "$ROOT_DIR"
bash build_scripts/build_llvm.sh
print_success "LLVM built successfully"
}
build_p4mlir() {
print_header "Building P4MLIR"
if [ -f "$P4MLIR_TRANSLATE" ]; then
print_warning "P4MLIR already built, skipping..."
return 0
fi
print_step "Building P4MLIR..."
cd "$P4C_DIR"
mkdir -p build
cd build
cmake .. -DENABLE_MLIR=ON
make -j$(nproc)
print_success "P4MLIR built successfully"
}
build_nutcracker() {
print_header "Building NutCracker"
print_step "Configuring CMake..."
mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR"
cmake .. -DCMAKE_BUILD_TYPE=Debug
print_step "Building NutCracker..."
make -j$(nproc)
print_success "NutCracker built successfully"
}
# ============================================================================
# Compilation Functions
# ============================================================================
compile_p4_to_mlir() {
local p4_file=$1
local output_file=$2
local force=${3:-false}
# Skip if MLIR exists and not forcing
if [ -f "$output_file" ] && [ "$force" = false ]; then
print_substep "MLIR already exists, skipping: $(basename $output_file)"
return 0
fi
print_step "P4 → P4HIR MLIR: $(basename $p4_file)"
print_substep "Input: $p4_file"
print_substep "Output: $output_file"
if [ ! -f "$P4MLIR_TRANSLATE" ]; then
print_error "P4MLIR translator not found. Please build P4MLIR first."
exit 1
fi
# Build P4 include paths
local include_args=""
if [ -d "$P4_INCLUDE_DIR" ]; then
include_args="-I$P4_INCLUDE_DIR"
fi
local p4c_include="$P4C_DIR/p4include"
if [ -d "$p4c_include" ]; then
include_args="$include_args -I$p4c_include"
fi
local app_dir=$(dirname "$p4_file")
if [ -d "$app_dir" ]; then
include_args="$include_args -I$app_dir"
fi
"$P4MLIR_TRANSLATE" $include_args --typeinference-only "$p4_file" > "$output_file" 2>&1
if [ $? -eq 0 ]; then
local line_count=$(wc -l < "$output_file")
local op_count=$(grep -c "p4hir\." "$output_file" || echo "0")
print_success "Generated P4HIR MLIR ($line_count lines, ~$op_count ops)"
else
print_error "P4 to MLIR compilation failed"
cat "$output_file"
exit 1
fi
}
run_partition_pass() {
local mlir_file=$1
local output_dir=$2 # Will be vdsa_output/
print_step "Step 2: Partitioning P4HIR into Basic Blocks"
print_substep "Input: $mlir_file"
print_substep "Output: $output_dir"
if [ ! -f "$NUTCRACKER_OPT" ]; then
print_error "nutcracker-opt not found. Please build NutCracker first."
exit 1
fi
if [ ! -f "$mlir_file" ]; then
print_error "Input MLIR file not found: $mlir_file"
exit 1
fi
# Get directories
local mlir_dir=$(dirname "$mlir_file")
local mlir_name=$(basename "$mlir_file")
# Change to MLIR directory and run pass
cd "$mlir_dir"
print_substep "Running partition pass (creates vdsa_output in current dir)..."
# Pass creates vdsa_output/ in current directory
"$NUTCRACKER_OPT" --p4hir-partition "$mlir_name" 2>&1
local result=$?
cd "$ROOT_DIR"
if [ $result -ne 0 ]; then
print_error "Partition pass failed"
return 1
fi
# Pass created vdsa_output in MLIR directory
local pass_output_dir="$mlir_dir/vdsa_output"
if [ ! -d "$pass_output_dir" ]; then
print_error "Pass did not create vdsa_output directory"
return 1
fi
# Move to ROOT_DIR/vdsa_output/
rm -rf "$output_dir"
mv "$pass_output_dir" "$output_dir"
if [ ! -d "$output_dir" ]; then
print_error "Failed to move output"
return 1
fi
# Count blocks
local block_count=$(find "$output_dir" -maxdepth 1 -type d -name "block*" 2>/dev/null | wc -l)
if [ "$block_count" -eq 0 ]; then
print_error "No blocks generated"
return 1
fi
print_success "Partitioned into $block_count basic blocks"
echo ""
}
# run_vffs_coarse_grained removed: P4HIRToVDRMT/VDPP now embed the
# coarse-grained finalization directly.
run_fine_grained_lowering() {
local mlir_file=$1
local output_dir=$2
print_step "Step 4a: Fine-Grained Lowering (vDRMT → bf3drmt)"
print_substep "Partition dir: $output_dir"
if [ ! -f "$NUTCRACKER_OPT" ]; then
print_error "nutcracker-opt not found."
exit 1
fi
if [ ! -d "$output_dir" ]; then
print_error "Partition output directory not found: $output_dir"
return 1
fi
cd "$ROOT_DIR"
# Export NUTCRACKER_ROOT so the pass can find the egglog template
export NUTCRACKER_ROOT="$ROOT_DIR"
print_substep "Running vDRMT → bf3drmt pass..."
"$NUTCRACKER_OPT" --vdrmt-to-bf3drmt "$mlir_file" 2>&1
local result=$?
if [ $result -ne 0 ]; then
print_warning "vDRMT fine-grained lowering reported errors (continuing)"
fi
local bf3drmt_count=$(find "$output_dir" -name "bf3drmt.mlir" 2>/dev/null | wc -l)
if [ "$bf3drmt_count" -gt 0 ]; then
echo -e " ${GREEN}✅ bf3drmt:${NC} $bf3drmt_count blocks"
for block_dir in "$output_dir"/block*; do
if [ -f "$block_dir/bf3drmt.mlir" ]; then
echo -e " ${GREEN}→${NC} $(basename $block_dir)/bf3drmt.mlir"
fi
done
else
echo -e " ${YELLOW}⚠️ bf3drmt:${NC} 0 blocks"
fi
echo ""
return 0
}
run_fine_grained_lowering_vdpp() {
local mlir_file=$1
local output_dir=$2
print_step "Step 4b: Fine-Grained Lowering (vDPP → bf3dpa)"
print_substep "Partition dir: $output_dir"
if [ ! -f "$NUTCRACKER_OPT" ]; then
print_error "nutcracker-opt not found."
exit 1
fi
if [ ! -d "$output_dir" ]; then
print_error "Partition output directory not found: $output_dir"
return 1
fi
# Check if any vdpp.mlir blocks exist
local vdpp_count=$(find "$output_dir" -name "vdpp.mlir" 2>/dev/null | wc -l)
if [ "$vdpp_count" -eq 0 ]; then
print_substep "No vDPP blocks found, skipping"
echo ""
return 0
fi
cd "$ROOT_DIR"
export NUTCRACKER_ROOT="$ROOT_DIR"
print_substep "Running vDPP → bf3dpa pass..."
"$NUTCRACKER_OPT" --vdpp-to-bf3dpa "$mlir_file" 2>&1
local result=$?
if [ $result -ne 0 ]; then
print_warning "vDPP fine-grained lowering reported errors (continuing)"
fi
local bf3dpa_count=$(find "$output_dir" -name "bf3dpa.mlir" 2>/dev/null | wc -l)
if [ "$bf3dpa_count" -gt 0 ]; then
echo -e " ${CYAN}✅ bf3dpa:${NC} $bf3dpa_count blocks"
for block_dir in "$output_dir"/block*; do
if [ -f "$block_dir/bf3dpa.mlir" ]; then
echo -e " ${CYAN}→${NC} $(basename $block_dir)/bf3dpa.mlir"
fi
done
else
echo -e " ${YELLOW}⚠️ bf3dpa:${NC} 0 blocks"
fi
[ -f "$output_dir/dpa_handler.c" ] && \
echo -e " ${CYAN}✅ dpa_handler.c generated${NC}"
[ -f "$output_dir/arm_handler.c" ] && \
echo -e " ${CYAN}✅ arm_handler.c generated${NC}"
echo ""
return 0
}
run_lowering_passes() {
local mlir_file=$1
local output_dir=$2
print_step "Step 3: Lowering P4HIR to Target Dialects"
print_substep "Partition dir: $output_dir"
if [ ! -f "$NUTCRACKER_OPT" ]; then
print_error "nutcracker-opt not found."
exit 1
fi
if [ ! -d "$output_dir" ]; then
print_error "Partition output directory not found: $output_dir"
return 1
fi
cd "$ROOT_DIR"
# Run vDRMT lowering (handles simple blocks)
print_substep "Trying vDRMT lowering (Match-Action)..."
"$NUTCRACKER_OPT" --p4hir-to-vdrmt "$mlir_file" 2>&1
local vdrmt_result=$?
# Run vDPP lowering (handles complex blocks)
print_substep "Trying vDPP lowering (Programmable)..."
"$NUTCRACKER_OPT" --p4hir-to-vdpp "$mlir_file" 2>&1
local vdpp_result=$?
# Check results
local vdrmt_count=$(find "$output_dir" -name "vdrmt.mlir" 2>/dev/null | wc -l)
local vdpp_count=$(find "$output_dir" -name "vdpp.mlir" 2>/dev/null | wc -l)
echo ""
print_substep "Lowering Results:"
if [ "$vdrmt_count" -gt 0 ]; then
echo -e " ${GREEN}✅ vDRMT:${NC} $vdrmt_count blocks"
for block_dir in "$output_dir"/block*; do
if [ -f "$block_dir/vdrmt.mlir" ]; then
echo -e " ${GREEN}→${NC} $(basename $block_dir)/vdrmt.mlir"
fi
done
else
echo -e " ${YELLOW}⚠️ vDRMT:${NC} 0 blocks"
fi
if [ "$vdpp_count" -gt 0 ]; then
echo -e " ${BLUE}✅ vDPP:${NC} $vdpp_count blocks"
for block_dir in "$output_dir"/block*; do
if [ -f "$block_dir/vdpp.mlir" ]; then
echo -e " ${BLUE}→${NC} $(basename $block_dir)/vdpp.mlir"
fi
done
else
echo -e " ${YELLOW}⚠️ vDPP:${NC} 0 blocks"
fi
local total_lowered=$((vdrmt_count + vdpp_count))
if [ "$total_lowered" -eq 0 ]; then
print_warning "No blocks were lowered to any target"
return 1
fi
print_success "Lowered $total_lowered blocks total"
echo ""
return 0
}
run_emit_handler_code() {
local mlir_file=$1
local output_dir=$2
print_step "Step 5.5: Emit Handler Code (mapping-aware)"
if [ ! -f "$NUTCRACKER_OPT" ]; then
print_error "nutcracker-opt not found."
return 1
fi
cd "$ROOT_DIR"
export NUTCRACKER_ROOT="$ROOT_DIR"
"$NUTCRACKER_OPT" --emit-handler-code "$mlir_file" 2>&1
local result=$?
if [ $result -ne 0 ]; then
print_warning "emit-handler-code reported errors (continuing)"
fi
[ -f "$output_dir/nc_types.h" ] && echo -e " ${CYAN}✅ nc_types.h${NC}"
[ -f "$output_dir/dpa_handler.c" ] && echo -e " ${CYAN}✅ dpa_handler.c${NC}"
[ -f "$output_dir/arm_handler.c" ] && echo -e " ${CYAN}✅ arm_handler.c${NC}"
[ -f "$output_dir/Makefile" ] && echo -e " ${CYAN}✅ Makefile${NC}"
echo ""
return 0
}
run_mapper() {
local output_dir=$1
local mode=${2:-balanced} # balanced | min-latency | max-throughput | min-energy
local mapper_egg="$output_dir/mapper.egg"
print_step "Step 5: Deployment Mapping (mapper)"
print_substep "Input: $mapper_egg"
print_substep "Mode: $mode"
if [ ! -f "$MAPPER" ]; then
print_warning "mapper binary not found: $MAPPER"
print_info "Build it with: cd deps/mapper && cargo build --release"
return 0
fi
if [ ! -f "$mapper_egg" ]; then
print_warning "mapper.egg not found (fine-grained lowering may have failed)"
return 0
fi
local mapper_out="$output_dir/mapping.txt"
"$MAPPER" "$mapper_egg" --mode "$mode" --per-objective > "$mapper_out" 2>&1
local result=$?
if [ $result -ne 0 ]; then
print_error "Mapper failed (exit $result)"
cat "$mapper_out" >&2
return 1
fi
# Print the mapping result inline
cat "$mapper_out"
print_success "Mapping written to $mapper_out"
echo ""
return 0
}
generate_visualization() {
local dot_file=$1
local output_pdf=$2
if [ ! -f "$dot_file" ]; then
print_warning "DOT file not found: $dot_file"
return 1
fi
print_step "Step 6: Generating Control Flow Visualization"
if command -v dot &> /dev/null; then
dot -Tpdf "$dot_file" -o "$output_pdf" 2>/dev/null
print_success "Generated PDF: $(basename $output_pdf)"
dot -Tpng "$dot_file" -o "${output_pdf%.pdf}.png" 2>/dev/null
print_success "Generated PNG: $(basename ${output_pdf%.pdf}.png)"
else
print_warning "Graphviz not installed. Skipping visualization."
print_info "Install with: sudo apt-get install graphviz"
fi
echo ""
}
# ============================================================================
# C App Functions (C → LLVM IR → vDRMT/vDPP)
# ============================================================================
precompile_c_app() {
local app_name=$1
local app_dir="$APPS_DIR/$app_name"
local c_file="$app_dir/${app_name}.c"
local ll_file="$app_dir/arm.ll"
local force=${2:-false}
if [ -f "$ll_file" ] && [ "$force" = false ]; then
print_info "LLVM IR already exists for $app_name (use --force to regenerate)"
return 0
fi
print_section "Precompiling C app: $app_name"
print_step "C → LLVM IR: $(basename $c_file)"
print_substep "Clang: $CLANG"
print_substep "Output: $ll_file"
if [ ! -f "$CLANG" ]; then
print_error "clang not found: $CLANG"
exit 1
fi
local ll_raw="$app_dir/arm_raw.ll"
local OPT="${OPT:-$ROOT_DIR/third_party/llvm/build/bin/opt}"
"$CLANG" -O0 -S -emit-llvm \
-fno-discard-value-names \
-o "$ll_raw" "$c_file" 2>&1
if [ $? -ne 0 ]; then
print_error "C compilation failed"
return 1
fi
# Promote alloca-based variables to SSA (simplifies provenance tracking).
# Strip 'optnone' first so mem2reg can run (it's added by -O0).
print_substep "Running mem2reg pass..."
sed 's/ optnone//g; s/noinline //g' "$ll_raw" | \
"$OPT" -passes=mem2reg -S -o "$ll_file" 2>&1
if [ $? -ne 0 ]; then
print_warning "mem2reg failed, using raw IR"
cp "$ll_raw" "$ll_file"
fi
local line_count=$(wc -l < "$ll_file")
print_success "Generated LLVM IR ($line_count lines)"
echo ""
}
compile_c_app() {
local app_name=$1
local app_dir="$APPS_DIR/$app_name"
local ll_file="$app_dir/arm.ll"
local output_dir="$OUTPUT_DIR"
print_header "Compiling C app: $app_name"
if [ ! -f "$ll_file" ]; then
print_error "LLVM IR not found: $ll_file (run precompile first)"
exit 1
fi
if [ ! -f "$NUTCRACKER_OPT" ]; then
print_error "nutcracker-opt not found. Please build NutCracker first."
exit 1
fi
rm -rf "$output_dir"
# Run vDPP lowering (non-fatal: some ops may be unhandled)
print_step "Step 1: LLVM IR → vDPP"
echo "" | "$NUTCRACKER_OPT" \
"--llvm-ir-to-vdpp=input-ll=$ll_file output-dir=$output_dir" \
/dev/null 2>&1 || true
local vdpp_result=$?
# Run vDRMT lowering
print_step "Step 2: LLVM IR → vDRMT"
# Run per block since each block has its own arm.ll from vDPP
local block_idx=0
for block_dir in "$output_dir"/block*; do
if [ -f "$block_dir/arm.ll" ]; then
echo "" | "$NUTCRACKER_OPT" \
"--llvm-ir-to-vdrmt=input-ll=$block_dir/arm.ll output-dir=$block_dir/.vdrmt_tmp" \
/dev/null 2>&1
# Move the generated vdrmt.mlir up from .vdrmt_tmp/block0/
if [ -f "$block_dir/.vdrmt_tmp/block0/vdrmt.mlir" ]; then
mv "$block_dir/.vdrmt_tmp/block0/vdrmt.mlir" "$block_dir/vdrmt.mlir"
fi
rm -rf "$block_dir/.vdrmt_tmp"
block_idx=$((block_idx + 1))
fi
done
# If vDPP didn't create arm.ll blocks (single-function C), run vDRMT on original
if [ "$block_idx" -eq 0 ]; then
print_substep "No per-block arm.ll found; running vDRMT on original LLVM IR"
mkdir -p "$output_dir/block0"
echo "" | "$NUTCRACKER_OPT" \
"--llvm-ir-to-vdrmt=input-ll=$ll_file output-dir=$output_dir/.vdrmt_tmp" \
/dev/null 2>&1
if [ -d "$output_dir/.vdrmt_tmp" ]; then
for b in "$output_dir/.vdrmt_tmp"/block*; do
local bname=$(basename "$b")
mkdir -p "$output_dir/$bname"
[ -f "$b/vdrmt.mlir" ] && mv "$b/vdrmt.mlir" "$output_dir/$bname/vdrmt.mlir"
done
rm -rf "$output_dir/.vdrmt_tmp"
fi
fi
# Summary
local vdrmt_count=$(find "$output_dir" -name "vdrmt.mlir" 2>/dev/null | wc -l)
local vdpp_count=$(find "$output_dir" -name "vdpp.mlir" 2>/dev/null | wc -l)
local block_count=$(find "$output_dir" -maxdepth 1 -type d -name "block*" 2>/dev/null | wc -l)
print_header "Compilation Summary"
echo -e "${BOLD}📦 Application:${NC} $app_name"
echo -e "${BOLD}🧩 Blocks:${NC} $block_count"
echo -e "${BOLD}📂 Output:${NC} $output_dir"
echo ""
echo -e " ${GREEN}vDRMT blocks:${NC} $vdrmt_count"
echo -e " ${BLUE}vDPP blocks:${NC} $vdpp_count"
echo ""
for block_dir in "$output_dir"/block*; do
if [ -d "$block_dir" ]; then
local bname=$(basename "$block_dir")
echo -e " ${CYAN}📦 $bname:${NC}"
[ -f "$block_dir/vdpp.mlir" ] && echo -e " ${BLUE}📄 vdpp.mlir${NC}"
[ -f "$block_dir/vdrmt.mlir" ] && echo -e " ${GREEN}📄 vdrmt.mlir${NC}"
[ -f "$block_dir/arm.ll" ] && echo -e " ${MAGENTA}📄 arm.ll${NC}"
fi
done
echo ""
print_success "Compilation complete!"
echo ""
}
# ============================================================================
# Precompile Functions
# ============================================================================
precompile_app() {
local app_name=$1
local app_dir="$APPS_DIR/$app_name"
local p4_file="$app_dir/${app_name}.p4"
local c_file="$app_dir/${app_name}.c"
local mlir_file="$app_dir/${app_name}.mlir"
local force=${2:-false}
# Dispatch to C path if no P4 but C exists
if [ ! -f "$p4_file" ] && [ -f "$c_file" ]; then
precompile_c_app "$app_name" "$force"
return $?
fi
if [ ! -f "$p4_file" ]; then
print_error "P4 file not found: $p4_file"
return 1
fi
if [ -f "$mlir_file" ] && [ "$force" = false ]; then
print_info "MLIR already exists for $app_name (use --force to regenerate)"
return 0
fi
print_section "Precompiling: $app_name"
compile_p4_to_mlir "$p4_file" "$mlir_file" "$force"
echo ""
}
precompile_all_apps() {
print_header "Precompiling All Applications (P4 → MLIR)"
local force=false
if [ "$1" = "--force" ]; then
force=true
print_info "Force regeneration enabled"
fi
setup_p4_includes
local success_count=0
local skip_count=0
local fail_count=0
for app_dir in "$APPS_DIR"/*; do
if [ -d "$app_dir" ]; then
local app_name=$(basename "$app_dir")
local p4_file="$app_dir/${app_name}.p4"
local mlir_file="$app_dir/${app_name}.mlir"
if [ -f "$p4_file" ]; then
if [ -f "$mlir_file" ] && [ "$force" = false ]; then
print_info "✓ $app_name (already compiled)"
((skip_count++))
else
if precompile_app "$app_name" "$force"; then
((success_count++))
else
((fail_count++))
print_error "Failed to precompile $app_name"
fi
fi
fi
fi
done
print_header "Precompile Summary"
echo -e " ${GREEN}✅ Compiled:${NC} $success_count"
echo -e " ${BLUE}⏭️ Skipped:${NC} $skip_count"
echo -e " ${RED}❌ Failed:${NC} $fail_count"
echo ""
if [ $success_count -gt 0 ]; then
print_success "Precompile complete! MLIR files ready for partition/lowering."
fi
}
# ============================================================================
# Compile Application
# ============================================================================
compile_app() {
local app_name=$1
local app_dir="$APPS_DIR/$app_name"
local p4_file="$app_dir/${app_name}.p4"
local c_file="$app_dir/${app_name}.c"
local mlir_file="$app_dir/${app_name}.mlir"
local output_dir="$OUTPUT_DIR"
# Dispatch to C path if no P4 but C exists
if [ ! -f "$p4_file" ] && [ -f "$c_file" ]; then
compile_c_app "$app_name"
return $?
fi
print_header "Compiling: $app_name"
if [ ! -f "$p4_file" ]; then
print_error "P4 file not found: $p4_file"
exit 1
fi
setup_p4_includes
# Step 1: P4 → MLIR (skip if exists)
compile_p4_to_mlir "$p4_file" "$mlir_file" false
rm -rf "$output_dir"
mkdir -p "$output_dir"
# Step 2: Partition into blocks
run_partition_pass "$mlir_file" "$output_dir"
# Step 3: Lower to both vDRMT and vDPP
# P4HIRToVDRMT/VDPP now embed the coarse-grained finalization (VFFA
# instance lifting + helper-attr stripping); no separate step needed.
run_lowering_passes "$mlir_file" "$output_dir"
# Step 4: Fine-grained lowering (target-specific)
if [ "$TARGET_BACKEND" = "bf2" ]; then
# BF2: vDRMT → bf2drmt (DOCA 2.2)
print_step "Step 4a: Fine-Grained Lowering (vDRMT → bf2drmt)"
DOCA_TEMPLATE="$ROOT_DIR/templates/doca-2.2.toml" \
"$NUTCRACKER_OPT" --vdrmt-to-bf2drmt 2>&1
else
# BF3 (default): vDRMT → bf3drmt + vDPP → bf3dpa
run_fine_grained_lowering "$mlir_file" "$output_dir"
run_fine_grained_lowering_vdpp "$mlir_file" "$output_dir"
fi
# Step 5: Deployment mapping
run_mapper "$output_dir" "${MAPPER_MODE:-balanced}"
# Step 5.5: Emit handler C code (mapping-aware) — BF3 only
# BF2's doca_flow_pipeline.c is already generated by --vdrmt-to-bf2drmt.
if [ "${TARGET_BACKEND:-bf3}" != "bf2" ]; then
run_emit_handler_code "$mlir_file" "$output_dir"
fi
# Step 6: Visualize
generate_visualization "$output_dir/dependency_graph.dot" "$output_dir/graph.pdf"
print_header "Compilation Summary"
if [ -d "$output_dir" ]; then
local block_count=$(find "$output_dir" -maxdepth 1 -type d -name "block*" | wc -l)
echo -e "${BOLD}📦 Application:${NC} $app_name"
echo -e "${BOLD}🧩 Blocks:${NC} $block_count"
echo -e "${BOLD}📂 Output:${NC} $output_dir"
echo ""
print_section "Block Statistics"
local block_id=0
local vdrmt_count=0
local vdpp_count=0
local bf3drmt_count=0
local unmapped_count=0
for block_dir in "$output_dir"/block*; do
if [ -f "$block_dir/metadata.json" ]; then
local ops=$(jq -r '.operations' "$block_dir/metadata.json" 2>/dev/null || echo "?")
local has_if=$(jq -r '.hasConditionalBranch' "$block_dir/metadata.json" 2>/dev/null || echo "false")
local has_table=$(jq -r '.hasTableApply' "$block_dir/metadata.json" 2>/dev/null || echo "false")
echo -e " ${CYAN}📦 Block $block_id:${NC} $ops ops"
[ "$has_if" = "true" ] && echo -e " ${YELLOW}🔀${NC} Conditional branch"
[ "$has_table" = "true" ] && echo -e " ${YELLOW}📊${NC} Table apply"
local has_vdrmt=false
local has_vdpp=false
if [ -f "$block_dir/vdrmt.mlir" ]; then
echo -e " ${GREEN}✅ vDRMT (Match-Action)${NC}"
echo -e " ${GREEN}📄 vdrmt.mlir${NC}"
has_vdrmt=true
vdrmt_count=$((vdrmt_count + 1))
fi
if [ -f "$block_dir/bf3drmt.mlir" ]; then
echo -e " ${MAGENTA}✅ bf3drmt (BF3 NIC ASIC)${NC}"
echo -e " ${MAGENTA}📄 bf3drmt.mlir${NC}"
bf3drmt_count=$((bf3drmt_count + 1))
fi
if [ -f "$block_dir/vdpp.mlir" ]; then
echo -e " ${BLUE}✅ vDPP (Programmable)${NC}"
echo -e " ${BLUE}📄 vdpp.mlir${NC}"
has_vdpp=true
vdpp_count=$((vdpp_count + 1))
fi
if [ -f "$block_dir/bf3dpa.mlir" ]; then
echo -e " ${CYAN}✅ bf3dpa (DPA/ARM)${NC}"
echo -e " ${CYAN}📄 bf3dpa.mlir${NC}"
fi
if [ "$has_vdrmt" = false ] && [ "$has_vdpp" = false ]; then
echo -e " ${RED}❌ Not lowered to any target${NC}"
unmapped_count=$((unmapped_count + 1))
fi
echo ""
block_id=$((block_id + 1))
fi
done
echo -e "${BOLD}Lowering Summary:${NC}"
echo -e " ${GREEN}vDRMT blocks:${NC} $vdrmt_count"
echo -e " ${MAGENTA}bf3drmt blocks:${NC} $bf3drmt_count"
echo -e " ${BLUE}vDPP blocks:${NC} $vdpp_count"
echo -e " ${RED}Unmapped:${NC} $unmapped_count"
echo -e " ${CYAN}Total blocks:${NC} $block_id"
echo ""
print_section "Generated Files"
echo -e "${BLUE}📁${NC} $output_dir/"
echo -e " ${MAGENTA}📄${NC} dependency_graph.dot"
[ -f "$output_dir/graph.pdf" ] && echo -e " ${MAGENTA}📄${NC} graph.pdf"
[ -f "$output_dir/graph.png" ] && echo -e " ${MAGENTA}📄${NC} graph.png"
[ -f "$output_dir/mapper.egg" ] && echo -e " ${CYAN}📄${NC} mapper.egg"
[ -f "$output_dir/mapping.txt" ] && echo -e " ${CYAN}📄${NC} mapping.txt"
[ -f "$output_dir/doca_flow_pipeline.c" ] && echo -e " ${CYAN}📄${NC} doca_flow_pipeline.c"
[ -f "$output_dir/dpa_handler.c" ] && echo -e " ${CYAN}📄${NC} dpa_handler.c"
[ -f "$output_dir/arm_handler.c" ] && echo -e " ${CYAN}📄${NC} arm_handler.c"
for block_dir in "$output_dir"/block*; do
if [ -d "$block_dir" ]; then
local block_name=$(basename "$block_dir")
echo -e " ${BLUE}📁${NC} $block_name/"
echo -e " ${MAGENTA}📄${NC} p4hir.mlir"
[ -f "$block_dir/vdrmt.mlir" ] && echo -e " ${GREEN}📄${NC} vdrmt.mlir"
[ -f "$block_dir/bf3drmt.mlir" ] && echo -e " ${MAGENTA}📄${NC} bf3drmt.mlir"
[ -f "$block_dir/vdpp.mlir" ] && echo -e " ${BLUE}📄${NC} vdpp.mlir"
[ -f "$block_dir/bf3dpa.mlir" ] && echo -e " ${CYAN}📄${NC} bf3dpa.mlir"
echo -e " ${MAGENTA}📄${NC} metadata.json"
fi
done
echo ""
fi